Get Domain from URL in Powershell

前端 未结 3 2033
旧时难觅i
旧时难觅i 2021-01-01 12:24

I\'m looking to strip out the domain in this scenario using PowerShell. What is the most effective method to get \'domain.com\' out of the following variable?



        
3条回答
  •  梦毁少年i
    2021-01-01 13:24

    Try the Uri class:

    PS> [System.Uri]"http://www.domain.com/folder/"
    
    
    AbsolutePath   : /folder/
    AbsoluteUri    : http://www.domain.com/folder/
    LocalPath      : /folder/
    Authority      : www.domain.com
    HostNameType   : Dns
    IsDefaultPort  : True
    IsFile         : False
    IsLoopback     : False
    PathAndQuery   : /folder/
    Segments       : {/, folder/}
    IsUnc          : False
    Host           : www.domain.com
    Port           : 80
    Query          :
    Fragment       :
    Scheme         : http
    OriginalString : http://www.domain.com/folder/
    DnsSafeHost    : www.domain.com
    IsAbsoluteUri  : True
    UserEscaped    : False
    UserInfo       :
    

    And remove the www prefix:

    PS> ([System.Uri]"http://www.domain.com/folder/").Host -replace '^www\.'
    domain.com
    

提交回复
热议问题