How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.
This question is related to
.net
powershell
hostname
also works just fine in Powershell
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
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
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
Source: Stackoverflow.com