Powershell

How to Get the Localhost Name in PowerShell?

Localhost is the default hostname set to access the current device. It uses a loopback service to track and access the running network services. Localhost’s IP address is by default “ 127.0.0.1”. At times, we need to know the Localhost name on Windows. For that purpose, you can use PowerShell. The command line interface is open-source and offers multiple commands that can be utilized to get the Localhost name.

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:

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:

> Systeminfo

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:

> Hostname

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:

> $Env:COMPUTERNAME

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:

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

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:

> [Environment]::MachineName

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:

> Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty Name

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:

> (Get-CimInstance -ClassName Win32_ComputerSystem).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.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.