Home » PowerShell » Using PowerShell to Get Computer Name and Domain

Using PowerShell to Get Computer Name and Domain

PowerShell provides a quick way to get computer name and other information like the domain name of the local computer. In this blog post, I will explain to you the possible best ways to get hostname of a local computer using different PowerShell cmdlets.

Let’s try out below PowerShell cmdlets to get computer name and domain name.

PowerShell Get Computer Name and Domain

You can get computer name using different commands available in PowerShell as given below

  • HostName.exe
  • WMI
  • Environment Variable
  • .Net MachineName
  • .Net GetHostName

Let’s check PowerShell to get a computer name using the above options.

Get Computer Name using HostName.exe

HostName.exe is an executable file available on your computer drive, should be located at C:\Windows\System32 . HostName.exe contains machine code and commands.

PS C:\>HostName.exe

Above command, HostName.exe gets the computer name or hostname in PowerShell.

Note: You can run the HostName.exe command on the command prompt to get computer name in PowerShell.

Cool Tip: How to use Get-AdDomainController to get domain controller in PowerShell!

Using WMI to get computer name

WMI (Windows Management Instrumentation) uses to access management information in a standard environment. WMI uses CIM (Common Information Model) standard to represents systems, applications, devices, networks, and many more.

Using PowerShell Get-WMIObject cmdlet, it can pull information about instances about WMI classes and information about available classes

#Get Computer system information
Get-WMIObject Win32_ComputerSystem

#Get Computer name from available System information
Get-WMIObject Win32_ComputerSystem| Select-Object -ExpandProperty Name

#Get Domain name from available System Information
Get-WMIObject Win32_ComputerSystem| Select-Object -ExpandProperty Domain

The first command returns information about the computer system like it get computer name, domain name, manufacturer, get computer model, etc.

The second command, Get-WMIObject Win32_ComputerSystem| Select-Object -ExpandProperty Name gets computer name using pipe operator over earlier command output.

The third command gets domain name using PowerShell.

Get Computer Name Using CIM

You can use PowerShell Get-CIMInstance to access common information model and returns information about the computer system

# Get computer information
Get-CIMInstance CIM_ComputerSystem

#Get Computer name only from available system information
(Get-CIMInstance CIM_ComputerSystem).Name

The above command returns computer system details like Name, PrimaryOwnerName, Domain name, computer model, Manufacturer, and many more. As we want just HostName, use Name to get computer name in PowerShell.

Get Host Name Using Environment variable

A very simple approach in PowerShell to get hostname is using the environment variable. Use $env to get the computer name

$env:computername

Using .Net Machine name

You can use .Net easily in PowerShell. With the help of .Net classes, you can easily get the machine name.

[system.environment]::MachineName

Using .Net GetHostName() function

As given above, .Net is easily accessed in PowerShell. You can use the .net provided class System.Net.Dns to get hostname using GetHostName() function.

[System.Net.Dns]::GetHostName()

Cool Tip: Do you know the cat equivalent command in Windows?

Conclusion

In the article, we have gone through different ways to get computer name or domain name using PowerShell.

I hope, you find the above examples are helpful to get hostname or domain name using PowerShell.

Read here to check if a computer is member of domain or workgroup using PowerShell.

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