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?
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