How to Get the Localhost Name in PowerShell

  1. Get Localhost Name Using Legacy Commands in PowerShell
  2. Get Localhost Name Using Environment Variables in PowerShell
  3. Get Localhost Name Using .NET Framework Library in PowerShell
  4. Get Localhost Name Using Windows Management Instrumentation in PowerShell
How to Get the Localhost Name in PowerShell

Localhost is a technical term that we usually use to define the hostname that refers to the current device used to access it. Therefore, there will be situations where we need to query for the current name of the local machine.

This article will explain several methods to get the localhost name of the computer using Windows PowerShell.

Get Localhost Name Using Legacy Commands in PowerShell

Many legacy Command Prompt (CMD) commands work in the Windows PowerShell scripting environment. The PowerShell environment carries these commands forward from the legacy environment using aliases.

One example of this is the command hostname. The command hostname will return the local computer’s name when running the command prompt.

Example Code:

hostname

Output:

WINDOWS-PC01

Get Localhost Name Using Environment Variables in PowerShell

Another method of getting the localhost name of your local machine is by calling one of our environment variables in PowerShell. Environment variables, depicted by Env: in Windows PowerShell, store the operating system environment and programs.

This information details include the active system path, location of the windows installation directory, number of processes used by the operating system, and so much more.

For this specific situation, we can use the environment variable Env:COMPUTERNAME to call the localhost name of our machine.

Get-Content Env:COMPUTERNAME

In the example above, we used the Get-Content cmdlet to get the value of the environment variable. We can simplify this by turning the environment variable into a Windows PowerShell variable.

$Env:COMPUTERNAME

Both sets of codes above should yield the same output, which would be the localhost name of the current machine you are using.

Get Localhost Name Using .NET Framework Library in PowerShell

In Windows PowerShell, the .NET Framework has its library that contains its own set of commands that we can call in our scripts. In addition, a couple of commands in the .NET Framework achieves the same goal in printing the localhost name of the computer.

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

Using the .NET framework library is usually discouraged from production use if there are available native commands in Windows PowerShell, but we can give an exception for this specific use case.

If you are joined in a domain, your DNS name will be much longer than a localhost machine name only joined into a local workgroup.

Therefore, if your DNS name is longer than 15 characters, [System.Net.Dns]::GetHostName(), where the output value will not truncate, is better than $env:COMPUTERNAME, which will trim the output.

Get Localhost Name Using Windows Management Instrumentation in PowerShell

Windows Management Instrumentation, or WMI, is the infrastructure for management data and operations on Windows-based operating systems.

WMI is most usually used in Windows-based applications and is most useful in administrative scripts, including essential functions such as printing values of the localhost name of the local machine.

Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty Name

The Get-WMIObject cmdlet holds multiple local machine information like the computer’s manufacturer, domain, and model. In addition, piping it only to export the Name property will return you a value of the localhost name of the local machine.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn