Get Domain from URL in PowerShell

Try the URI class:

PS> [System.Uri]"http://www.example.com/folder/"

AbsolutePath   : /folder/
AbsoluteUri    : http://www.example.com/folder/
LocalPath      : /folder/
Authority      : www.example.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /folder/
Segments       : {/, folder/}
IsUnc          : False
Host           : www.example.com
Port           : 80
Query          :
Fragment       :
Scheme         : http
OriginalString : http://www.example.com/folder/
DnsSafeHost    : www.example.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

And remove the www prefix:

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

Leave a Comment