PowerShell Command to Get Computer Name (Hostname)
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
On any Windows operating system, you can run the
hostname command to find the computer name:
The following was the output when I executed the
hostname command on my Windows 10 PC:
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.
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:
To get the computer name without the header, run the
Get-ComputerInfo command as follows:
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
systeminfo is another command that we can use to display the hostname on Windows:
systeminfo | findstr /i /c:"host name"
systeminfo is a CMD command. However, you can also run it on Windows PowerShell.
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.