Powershell – Find computer name and domain

Sometimes it’s useful to know the computer name and domain you are working on. The first of the following commands returns the FQDN of the computer on the format whereas the second one returns a list of information about the system. The following ones are some of the ways to gather the domain and computer name using Powershell:

“$env:COMPUTERNAME.$env:USERDNSDOMAIN”
Get-WmiObject Win32_ComputerSystem

The first one returns the FQDN of the computer on the format whereas the second one returns a list of information about the computer such as NETBIOS name (that could be different from the DNS name), domain name, phisical memory, etc. You can also select single properties, changing the cmdlet this way:

(Get-WmiObject Win32_ComputerSystem).Name
(Get-WmiObject Win32_ComputerSystem).Domain
## and so on…

Another way is to use the Active Directory Powershell Module (or just connecting on a Domain Controller in your own domain), and using the following command to gather the domain name:

Get-ADDomain | Select-Object -ExpandProperty DNSRoot

That’s all.