To get current username in PowerShell, use whoami, GetCurrent() method of WindowsIdentity . Net class or Get-WMIObject cmdlet in PowerShell.
From the Start menu, select All Programs or Programs, then Accessories, and then Command Prompt. In the window that opens, at the prompt, enter hostname . The result on the next line of the command prompt window will display the hostname of the machine without the domain.
$_ is an alias for automatic variable $PSItem (introduced in PowerShell V3. 0; Usage information found here) which represents the current item from the pipe. PowerShell (v6.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With