Config Server Firewall

PowerShell Command to Get Computer Name (Hostname)

Computer Name

In this tutorial, we will look at a few PowerShell commands we can use to get the computer name in the Windows operating system. One command is the hostname. Another one is Get-ComputerInfo.

On any Windows operating system, you can run the hostname command to find the computer name:

hostname

The following was the output when I executed the hostname command on my Windows 10 PC:

powershell get hostname

Note that the hostname command is not a native PowerShell Cmdlet. It is a CMD command. So it will work on both PowerShell and Windows CMD.

With the Get-ComputerInfo, the CsName property holds the computer name:

Get-ComputerInfo -Property CsName

The following was the output when I executed the above command on my Windows 11 laptop:

powershell get computer name

To get the computer name without the header, run the Get-ComputerInfo command as follows:

(Get-ComputerInfo).CsName

In the following example, we retrieve the computer name from the Win32_ComputerSystem CIM (Common Information Model) class:

Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object DNSHostName

This command shows the Domain or Workgroup the computer belongs to:

Get-ComputerInfo -Property CsDomain

The systeminfo is another command that we can use to display the hostname on Windows:

systeminfo | findstr /i /c:"host name"

The systeminfo is a CMD command. However, you can also run it on Windows PowerShell.

What Next?

This concludes this tutorial on how to get the computer name in Windows PowerShell. Next, we will learn how to rename the computer using the Rename-Computer Cmdlet.