Home » PowerShell Tips » Get Domain name using PowerShell and CMD

Get Domain name using PowerShell and CMD

In a large organization, it very quite common to have many domain and child domain names. While performing task automation for a set of computers in the domain, it’s best practice to get domain name of a computer.

In this article, I will explain how to get domain name using the PowerShell script and command line (CMD)

Get-WmiObject class in the PowerShell management library finds the domain name of a computer and the wmic command-line utility to get domain name using the command line (cmd).

You can also get FQDN (Fully Qualified Domain Name) in PowerShell using the environment variable.

Let’s understand how to get domain name in PowerShell and the command line with the below examples.

PowerShell Get Domain name

You can use Get-WmiObject class in PowerShell.Management gets WMI classes in the root namespace of the computer and gets current domain name for a local computer

 Get-WmiObject -Namespace root\cimv2 -Class Win32_ComputerSystem | Select Name, Domain

In the above PowerShell script, Get-WmiObject gets the WMI classes in the root\cimv2 namespace of computer and uses Win32_ComputerSystem to get computer system information.

The second command uses the Select command to display the Name and current Domain name of a computer.

The output of the above command to get domain name of a computer is below.

PowerShell Get Domain Name
PowerShell Get Domain Name

Using Get-AdDomainController to get domain name

PowerShell Get-AdDomainController cmdlet in Active Directory get one or more domain controllers based on search criteria.

You can get domain name of a computer in an active directory using PowerShell Get-AdDomainController cmdlet as below

 Get-ADDomainController -Identity "ENGG-PRO" | Select-Object Name, Domain

In the above PowerShell script, the Get-AdDomainController command get domain controller specified by the name of a server object

The second command uses the Select command to select the name and domain name, output as below

PS C:\Windows\system32> Get-ADDomainController -Identity "ENGG-PRO" | Select-Object Name, Domain

Name     Domain
----     ------
ENGG-PRO SHELLPRO.LOCAL


PS C:\Windows\system32>

Use Get-AdDomain to Get Domain Distinguished Name in PowerShell

You can get domain distinguished name for the current logged-in user in the active directory using PowerShell as below

Get-ADDomain -Current LoggedOnUser

PowerShell Get-ADDomain cmdlet finds domain name in the active directory for the currently logged-on user.

The output of the above command to get domain distinguished name is below

PS C:\Windows\system32> Get-ADDomain -Current LoggedOnUser


AllowedDNSSuffixes                 : {}
ChildDomains                       : {}
ComputersContainer                 : CN=Computers,DC=SHELLPRO,DC=LOCAL
DeletedObjectsContainer            : CN=Deleted Objects,DC=SHELLPRO,DC=LOCAL
DistinguishedName                  : DC=SHELLPRO,DC=LOCAL
DNSRoot                            : SHELLPRO.LOCAL

PowerShell Get FQDN (Fully Qualified Domain Name)

In PowerShell, the environment variable $env:$USERDNSDomain contains the FQDN ( fully qualified domain name) of a computer.

These variables are $env:USERDNSDomain and $env:$USERDomain

$env:USERDNSDomain variable contains FQDN ( fully qualified domain name) of domain or DNS name

$env:USERDomain variable contains the NetBIOS domain name.

# Get Domain name using $env:USERDNSDoman 
# PowerShell Get FQDN - Fully Qualified Domain Name or DNS name

$env:USERDNSDOMAIN

#Get NetBios Domain name

$env:USERDOMAIN

The output of the above environment variable to get domain name is as below

Find Domain Name using env:USERDNSDOMAIN
Find Domain Name using env:USERDNSDOMAIN

Get Domain Name using Command Line

You can use wmic command-line utility to get domain name using the command line.

Run the below command in cmd to retrieve domain name

wmic computersystem get domain

The output of the above command to find domain name using cmd is below

C:\Windows\system32>wmic computersystem get domain
Domain
SHELLPRO.LOCAL

Find Domain Name using SystemInfo in CMD

You can get domain name using systeminfo which contains detailed information about the computer system and operating system, Run the below command.

systeminfo | findstr /B /C:"Domain"

The above SystemInfo command gets domain name of a computer joined to. The output of the above command is below

C:\Windows\system32>systeminfo | findstr /B /C:"Domain"
Domain:                    SHELLPRO.LOCAL

Conclusion

In the above article, we have learned how to get the domain of a computer using PowerShell and the command line.

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 tools are useful to get domain name in cmd.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment