How do I get the localhost name in PowerShell?
Categories:
How to Retrieve the Local Hostname in PowerShell

Learn various methods to obtain the local computer's hostname using PowerShell, covering built-in cmdlets, environment variables, and .NET classes.
Knowing your local hostname is fundamental for network configuration, scripting, and system administration tasks. PowerShell, being a powerful automation tool for Windows, offers several straightforward ways to retrieve this information. This article will guide you through the most common and effective methods, explaining their nuances and best use cases.
Understanding Hostnames
A hostname is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web. On a local network, it helps in identifying your specific machine. In PowerShell, you might need the hostname for logging, dynamic path creation, or interacting with other network resources.
flowchart TD A[Start] --> B{Need Local Hostname?} B -->|Yes| C[Use PowerShell Cmdlets] C --> D[Get-ComputerInfo] C --> E[Get-WmiObject Win32_ComputerSystem] B -->|Yes| F[Use Environment Variables] F --> G[env:COMPUTERNAME] B -->|Yes| H[Use .NET Classes] H --> I[[System.Net.Dns]::GetHostName()] D --> J[Output Hostname] E --> J G --> J I --> J J --> K[End]
Flowchart of methods to retrieve local hostname in PowerShell
Method 1: Using the $env:COMPUTERNAME Environment Variable
The simplest and often most direct way to get the local hostname in PowerShell is by accessing the built-in environment variable $env:COMPUTERNAME
. This variable is automatically populated by the operating system and provides the computer's NetBIOS name.
$env:COMPUTERNAME
Retrieving hostname using an environment variable
Method 2: Using the Get-ComputerInfo Cmdlet
For PowerShell 5.1 and later, the Get-ComputerInfo
cmdlet provides a wealth of information about the local computer, including its hostname. This cmdlet is part of the Microsoft.PowerShell.Management module and offers a structured way to retrieve system details.
(Get-ComputerInfo).CsName
Extracting hostname using Get-ComputerInfo
Get-ComputerInfo
cmdlet returns an object with many properties. We specifically access the CsName
property to get the computer's name.Method 3: Using WMI (Get-WmiObject or Get-CimInstance)
Windows Management Instrumentation (WMI) provides a powerful way to query system information. You can use Get-WmiObject
(older PowerShell versions) or Get-CimInstance
(recommended for PowerShell 3.0+) with the Win32_ComputerSystem
class to retrieve the hostname.
Get-WmiObject (Legacy)
(Get-WmiObject Win32_ComputerSystem).Name
Get-CimInstance (Modern)
(Get-CimInstance Win32_ComputerSystem).Name
Get-WmiObject
still works, Get-CimInstance
is the preferred cmdlet for interacting with WMI/CIM in modern PowerShell environments due to improved performance and compatibility.Method 4: Using .NET Framework Classes
PowerShell allows direct access to the .NET Framework. The System.Net.Dns
class provides a static method GetHostName()
which returns the local computer's hostname.
[System.Net.Dns]::GetHostName()
Retrieving hostname using .NET's System.Net.Dns class