How do I get the localhost name in PowerShell?

和自甴很熟 提交于 2021-02-04 09:09:04

问题


How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.


回答1:


You can just use the .NET Framework method:

[System.Net.Dns]::GetHostName()

also

$env:COMPUTERNAME




回答2:


Don't forget that all your old console utilities work just fine in PowerShell:

PS> hostname
KEITH1



回答3:


Long form:

get-content env:computername

Short form:

gc env:computername



回答4:


All above questions are correct but if you want the hostname and domain name try this:

 [System.Net.DNS]::GetHostByName('').HostName



回答5:


A slight tweak on @CPU-100's answer, for the local FQDN:

[System.Net.DNS]::GetHostByName($Null).HostName




回答6:


In PowerShell Core v6 (works on macOS, Linux and Windows):

[Environment]::MachineName



回答7:


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



回答8:


hostname also works just fine in Powershell



来源:https://stackoverflow.com/questions/1169891/how-do-i-get-the-localhost-name-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!