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?
$URL = "http://www.domain.com/folder/"
(some sort of regex command here to convert/strip $URL into $DOMAIN using PowerShell)
$DOMAIN = "domain.com" #<-- taken from $URL
I've searched and I've found results for finding the IP address from a domain but I need to establish what the domain is first using regex (or another method). Any suggestions are great.
Use Get-WmiObject to get domain name of a computer using PowerShell. Using Get-AdDomainController get domain name in active directory. wmic and SystemInfo command-line tool are useful to get domain name in cmd.
You can use the Get-ADDomainController PowerShell cmdlet to get information about the domain controllers in Active Directory. This cmdlet is a part of PowerShell Active Directory module and requires RSAT installation (onWindows 10 1809 and newer RSAT is installed in a different way).
The Get-ADDomain cmdlet gets the Active Directory domain specified by the parameters. You can specify the domain by setting the Identity or Current parameters. The Identity parameter specifies the Active Directory domain to get.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With