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:

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:

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:

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:
- Using the Environment Variable
- Using WMI (Windows Management Instrumentation)
- Using CIM (Common Information Model)
- Using .NET Classes
- Using the Get-ADComputer Cmdlet
I hope all these methods will be helpful.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.