Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Domain from URL in Powershell

Tags:

powershell

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.

like image 717
Mike J Avatar asked Jan 16 '13 16:01

Mike J


People also ask

How do I get a domain name in PowerShell?

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.

How do I find the domain controller in PowerShell?

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).

How do I use ADDomain?

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.


1 Answers

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 
like image 162
Shay Levy Avatar answered Sep 30 '22 19:09

Shay Levy