Powershell

How to Use PowerShell to Get Computer Name in No Time

PowerShell is a useful utility used for a variety of purposes such as performing automation and administrative tasks. In PowerShell, the currently logged-in user can get the computer name. More specifically, knowing the computer name in which you are currently logged in is very handy and useful for performing several tasks. For this purpose, PowerShell offers multiple commands.

This write-up will observe several approaches to retrieve the computer name.

How to Use/Utilize the PowerShell to Get/Retrieve the Name of Computer?

These methods can be implemented to get/retrieve the computer’s name:

Method 1: Use the “Hostname.exe” Command to Get the Computer Name

The simplest way to check the computer name is to execute the “Hostname.exe” command, as demonstrated below:

> Hostname.exe

Method 2: Use “ComputerName” Environment Variable to Get the Computer Name

Each system has an environment variable which is “COMPUTERNAME”. This variable displays the computer name when executed in the PowerShell console:

> $env:COMPUTERNAME

Method 3: Use the “GetHostName()” Command to Get the Computer Name

Here is another approach that we can use to retrieve the computer name by invoking the “GetHostName()” method. It is a “[System.Net.Dns]” class method that can be executed for the mentioned purpose:

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

Method 4: Use the “MachineName” Property to Retrieve/Get the Name of Computer

The “MachineName” property is also utilized to retrieve the computer name. The “MachineName” property is a .Net Environment class’s part.

Let’s execute the below-mentioned command to get the computer’s name:

> [Environment]::MachineName

Method 5: Use the “WMI” .Net Class to Get the Computer Name

The “WMI (Windows Management Instrumentation)” retrieves the information of the management in the standard environment. It is used to get the computer name with the combination of some other commands and attributes:

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

That was all about using PowerShell for getting the computer name in no time.

Conclusion

The computer’s name in PowerShell can be retrieved using several methods. These methods include “Hostname.exe”, “ComputerName Environment Variable”, “GetHostName()”, “MachineName Property”, and “WMI”. This write-up has covered multiple cmdlets to get the computer’s 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.