How to Get Computer Name in PowerShell?

Recently, someone asked me to get the computer’s name in PowerShell. I tried different methods. In this tutorial, I will show you how to get the computer name in PowerShell using various cmdlets.

To get the computer name in PowerShell, you can use several methods. The simplest is using the environment variable $env:COMPUTERNAME. Alternatively, you can use Get-WmiObject -Class Win32_ComputerSystem or Get-CimInstance -ClassName Win32_ComputerSystem to query the system for its name.

1. Using the Environment Variable

The simplest and quickest way to get the computer name in Powershell is by using the environment variable $env:COMPUTERNAME. This will work across different versions of Windows.

Here is an example:

$computerName = $env:COMPUTERNAME
Write-Output "Computer Name: $computerName"

This command reads the COMPUTERNAME environment variable and stores it in the $computerName variable. It then outputs the computer name to the console.

I executed the above PowerShell script, and you can see the output in the screenshot below:

Get Computer Name in PowerShell

Check out PowerShell ForEach Loop

2. Using WMI (Windows Management Instrumentation)

WMI is a powerful feature in Windows that allows you to query various system information. You can use the Get-WmiObject cmdlet to retrieve the computer name.

Here is an example.

$computerName = (Get-WmiObject -Class Win32_ComputerSystem).Name
Write-Output "Computer Name: $computerName"

In this example, Get-WmiObject queries the Win32_ComputerSystem class, and the Name property of the returned object contains the computer name.

Here is the output in the screenshot below:

How to Get Computer Name in PowerShell

3. Using CIM (Common Information Model)

The CIM cmdlets are the modern replacement for WMI cmdlets and provide similar functionality. You can use the Get-CimInstance cmdlet to get the computer name.

Here is an example and the complete code.

$computerName = (Get-CimInstance -ClassName Win32_ComputerSystem).Name
Write-Output "Computer Name: $computerName"

This command works similarly to the WMI example but uses the Get-CimInstance cmdlet instead.

You can see the output in the screenshot below after I executed the above PowerShell script:

PowerShell Get Computer Name

Check out How to Add Comments in PowerShell?

4. Using .NET Classes

PowerShell can leverage .NET classes to perform various tasks. You can use the [System.Environment] class to get the computer name.

You can see the example and the complete script.

$computerName = [System.Environment]::MachineName
Write-Output "Computer Name: $computerName"

Here, the MachineName property of the [System.Environment] class is used to retrieve the computer name.

5. Using the Get-ADComputer Cmdlet

If you are working in an Active Directory environment, you can use the Get-ADComputer cmdlet to get the computer name. This method requires the Active Directory module to be installed and imported.

Here is an example.

Import-Module ActiveDirectory
$computerName = (Get-ADComputer -Identity $env:COMPUTERNAME).Name
Write-Output "Computer Name: $computerName"

In this example, the Get-ADComputer cmdlet is used to query Active Directory for the computer object, and the Name property is retrieved.

Conclusion

In this tutorial, I have explained how to get the computer name using PowerShell using the below method:

  1. Using the Environment Variable
  2. Using WMI (Windows Management Instrumentation)
  3. Using CIM (Common Information Model)
  4. Using .NET Classes
  5. Using the Get-ADComputer Cmdlet

I hope all these methods will be helpful.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.