How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.
11 Answers
You can just use the .NET Framework method:
[System.Net.Dns]::GetHostName()
also
$env:COMPUTERNAME

- 50,229
- 9
- 102
- 115
-
9Note: if your DNS name is longer than 15 characters, `[System.Net.Dns]::GetHostName()` (doesn't truncate) is better than `$env:COMPUTERNAME` (truncates) – sonjz Jul 19 '16 at 23:42
-
On Windows 10 Enterprise, env:COMPUTERNAME produced the following error: env:COMPUTERNAME : The term 'env:COMPUTERNAME' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + env:COMPUTERNAME + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (env:COMPUTERNAME:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException – mathisfun Mar 14 '18 at 12:21
-
3@mathisfun `$env:COMPUTERNAME` works fine on Windows 10 (please note dollar sign at the beginning) – oleksa Jun 19 '18 at 08:08
-
2`$env:COMPUTERNAME` won't work on Linux and macOS. However, `[Environment]::MachineName` does. – felixfbecker May 30 '19 at 14:51
Don't forget that all your old console utilities work just fine in PowerShell:
PS> hostname
KEITH1

- 6,634
- 4
- 38
- 90
-
This is a good answer, however, if you want to save the value in a variable then use the accepted answer. – Yawar Murtaza Sep 29 '19 at 20:17
-
2
Long form:
get-content env:computername
Short form:
gc env:computername

- 69,610
- 20
- 126
- 152
-
22
-
This will work fine, until you have a server with a name longer than 15 characters. – Gary Pendlebury Jun 07 '18 at 09:41
-
-
@Sajuuk `computername` is the NetBIOS name, which is limited to 15 characters. – Gary Pendlebury May 31 '19 at 12:11
All above questions are correct but if you want the hostname and domain name try this:
[System.Net.DNS]::GetHostByName('').HostName

- 21,260
- 6
- 105
- 81
A slight tweak on @CPU-100's answer, for the local FQDN:
[System.Net.DNS]::GetHostByName($Null).HostName

- 336
- 5
- 8
In PowerShell Core v6 (works on macOS, Linux and Windows):
[Environment]::MachineName

- 2,273
- 1
- 19
- 24
hostname
also works just fine in Powershell

- 73
- 2
- 6
-
I can confirm that this works on PowerShell Core 7.3.1 on macOS as well as PowerShell on Windows 10. – Van Vangor Jan 28 '23 at 20:54
An analogue of the bat file code in Powershell
Cmd
wmic path Win32_ComputerSystem get Name
Powershell
Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
and ...
hostname.exe

- 591
- 3
- 10
Not specifically for Powershell version 1.0 and more of an overview of different possible ways to get information via Powershell:
- Native CmdLet (usually the best, but sadly really slow to get the Computername):
(Get-ComputerInfo).CsDNSHostName
Returns the "Name of local computer according to the domain name server" (Info aboutGet-ComputerInfo
, Info about its return value) - Classical CommandLine executable:
hostname.exe
Returns the "host name portion of the full computer name of the computer" (Info abouthostname.exe
- Environment Variable:
$env:COMPUTERNAME
- Registry:
(Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName).ComputerName
- Static .Net Property:
[Environment]::MachineName
Returns "the NetBIOS name of this local computer." (Info about theSystem.Environment
class) - Static .Net Method:
[System.Net.Dns]::GetHostEntry("").HostName
Returns the DNS Hostname (fqdn) of the system (Info about theSystem.Net.Dns.GetHostEntry
Method) - Property of COM-Object (Computername via deprecated Scripting-Api):
(New-Object -ComObject WScript.Network).ComputerName
Returns "the name of the computer system." (Info about the WshNetwork Object, Info about the Windows Script Host Objects) - Property of WMI-Object:
(Get-CimInstance -ClassName Win32_ComputerSystem).Name
(Info about Win32_ComputerSystem) - P/Invoke a native API function:
Add-Type -TypeDefinition @'
public enum COMPUTER_NAME_FORMAT{
ComputerNameNetBIOS,
ComputerNameDnsHostname,
ComputerNameDnsDomain,
ComputerNameDnsFullyQualified,
ComputerNamePhysicalNetBIOS,
ComputerNamePhysicalDnsHostname,
ComputerNamePhysicalDnsDomain,
ComputerNamePhysicalDnsFullyQualified,
ComputerNameMax,
}
public static class Kernel32{
[System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, System.Text.StringBuilder lpBuffer, ref uint lpnSize);
}
'@
$len = 0
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $null, [ref]$len); #get required size
$sb = [System.Text.StringBuilder]::new([int]$len) #create StringBuilder with required capacity (ensure int constructor is used, otherwise powershell chooses string constructor for uint value...)
$len = $sb.Capacity #get actual capacity of StringBuilder (important, as maybe StringBuilder was constructed differently than expected)
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $sb, [ref]$len);
$sb.ToString()
Returns "The fully qualified DNS name that uniquely identifies the local computer. This name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the cluster virtual server."
(Info about the GetComputerNameEx
API function, Info about the COMPUTER_NAME_FORMAT
enumeration, used c# signature of COMPUTER_NAME_FORMAT
(unofficial), used c# signature of GetComputerNameEx
(unofficial), short blogpost about P/Invoke in powershell)
Note: Use P/Invoke with caution. Sufficiently incorrect usage can actually make powershell crash. The Add-Type
call is somewhat slow (but only needs to be called once in the script).

- 1,656
- 18
- 26
The most descriptive way for me is:
[System.Net.DNS]::GetHostByName($env:COMPUTERNAME).HostName

- 877
- 10
- 12
You can store the value as follows
$name = $(hostname)
I want to add that simply executing $name = hostname
will also save the localhost name of the PC into a variable.

- 35
- 1
- 10
-
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30764121) – Emil Sierżęga Jan 13 '22 at 09:58