How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.
You can just use the .NET Framework method:
[System.Net.Dns]::GetHostName()
also
$env:COMPUTERNAME
Don't forget that all your old console utilities work just fine in PowerShell:
PS> hostname KEITH1
Long form:
get-content env:computername
Short form:
gc env:computername
All above questions are correct but if you want the hostname and domain name try this:
[System.Net.DNS]::GetHostByName('').HostName
A slight tweak on @CPU-100's answer, for the local FQDN:
[System.Net.DNS]::GetHostByName($Null).HostName
In PowerShell Core v6 (works on macOS, Linux and Windows):
[Environment]::MachineName
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
hostname also works just fine in Powershell
hostname
The most descriptive way for me is:
[System.Net.DNS]::GetHostByName($env:COMPUTERNAME).HostName