How to Get the Computer Name in PowerShell (7 and 5.1)

The fastest way to get the computer name in PowerShell is the COMPUTERNAME environment variable. Type this and press Enter:

$env:COMPUTERNAME

It works the same in PowerShell 7 and Windows PowerShell 5.1 on Windows. The other methods below return almost the same thing, but they differ in letter case, in remote support, and on Linux or macOS.

I ran every example on my own PC in both shells. In the text outputs, I replaced my real computer name with DESKTOP-4T7K2QX.

Four one-line ways to get the computer name

Each of these returns the local computer name as a plain string, so you can store it in a variable or drop it into a file name:

$env:COMPUTERNAME
hostname
[System.Net.Dns]::GetHostName()
[Environment]::MachineName

Output:

DESKTOP-4T7K2QX
DESKTOP-4T7K2QX
DESKTOP-4T7K2QX
DESKTOP-4T7K2QX

$env:COMPUTERNAME reads an environment variable, and hostname runs the classic hostname.exe tool. The two .NET calls ask the operating system directly, so they still work if someone changed the variable in the session.

Because the result is just text, you can reuse it like any other value. The guide to setting and getting environment variables explains where COMPUTERNAME comes from.

Why do hostname and $env:COMPUTERNAME show different letter case?

On my PC the outputs looked identical after redaction, but they aren’t. Comparing each method to $env:COMPUTERNAME with -eq (ignores case) and -ceq (case-sensitive) shows the difference:

$envName = $env:COMPUTERNAME
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
$checks = [ordered]@{
    'hostname'                = (hostname)
    'Dns.GetHostName()'       = [System.Net.Dns]::GetHostName()
    'Environment.MachineName' = [Environment]::MachineName
    'CIM Name'                = $cs.Name
    'CIM DNSHostName'         = $cs.DNSHostName
}
foreach ($k in $checks.Keys) {
    $v = $checks[$k]
    $fmt = '{0,-24} -eq: {1,-5}  -ceq: {2}'
    $fmt -f $k, ($v -eq $envName), ($v -ceq $envName)
}

Output:

hostname                 -eq: True   -ceq: False
Dns.GetHostName()        -eq: True   -ceq: False
Environment.MachineName  -eq: True   -ceq: True
CIM Name                 -eq: True   -ceq: True
CIM DNSHostName          -eq: True   -ceq: False
PowerShell compares computer name methods with -eq and -ceq
hostname, GetHostName() and DNSHostName keep the mixed case (VS Code, PowerShell 7)

$env:COMPUTERNAME, [Environment]::MachineName and the CIM Name property return the NetBIOS name, which Windows always stores in uppercase. hostname, GetHostName() and DNSHostName return the DNS host name, which keeps the case you typed when you named the PC.

So compare names with -eq, never -ceq. If you need one consistent form, convert the name to uppercase or lowercase first.

Get the computer name with Get-CimInstance Win32_ComputerSystem

The Win32_ComputerSystem class holds the name plus the domain details, which is handy when one call should answer several questions:

Get-CimInstance -ClassName Win32_ComputerSystem |
    Select-Object Name, DNSHostName, Domain, PartOfDomain |
    Format-List

Output:

Name         : DESKTOP-4T7K2QX
DNSHostName  : DESKTOP-4T7K2QX
Domain       : WORKGROUP
PartOfDomain : False