This post will overview various approaches to getting the Localhost name.
How to Get/Retrieve the Localhost Name Using PowerShell?
The following commands can be utilized to find out the localhost name:
- Systeminfo command.
- Hostname command.
- $Env:COMPUTERNAME command.
- [System.Net.Dns]::GetHostName() command.
- [Environment]::MachineName command.
- Get-WMIObject command.
- Get-CimInstance command.
Method 1: Getting the Localhost Name in PowerShell Using “Systeminfo” Command
The “Systeminfo” cmdlet is used to display detailed information about the system, including computer name, operating system name, and other system information. It can also be utilized for fetching the Localhost name.
For that reason, execute the given cmdlet:
Method 2: Getting the Localhost Name in PowerShell Using “Hostname” Command
The term “Hostname” is a name assigned to a device/computer that is hooked to the internet. When the “Hostname” command is executed in PowerShell, it outputs the localhost name:
Method 3: Getting the Localhost Name in PowerShell Using “$Env:COMPUTERNAME” Command
The environment variable “$Env” can be utilized to get/retrieve the localhost name. For this purpose, mention the “COMPUTERNAME” in the given command as follows:
Method 4: Getting the Localhost Name in PowerShell Using “[System.Net.Dns]::GetHostName()” Command
The localhost name can be retrieved by invoking the “GetHostName()” method of the “System.Net.Dns” static class:
Method 5: Getting the Localhost Name in PowerShell Using “[Environment]::MachineName” Command
Another method to get the localhost name is to access the value of the “[Environment]::Machine” property in PowerShell:
Method 6: Getting the Localhost Name in PowerShell Using “Get-WMIObject ” Command
In the below-given command, the “Get-WMIObject” command will query the data in the “Win32_ComputerSystem”. Then, it sends the output as an input to the “Select-Object” command using the Pipeline operator “|”, which then expands the value of the “Name” property:
As you can see the local hostname has been printed in the output.
Method 7: Getting the Localhost Name in PowerShell Using “Get-CimInstance” Command
The given command is also a “Wind32_ComputerSystem” CimInstance cmdlet, which can be utilized to get the localhost name:
Conclusion
The localhost name can be obtained by executing several commands in PowerShell. These commands include systeminfo, hostname, $Env: COMPUTERNAME, [System.Net.Dns]:: GetHostName(), [Environment]::MachineName, Get-WMIObject command, or Get-CimInstance command. This tutorial has presented multiple methods to get the localhost name in PowerShell.