如何在PowerShell中获得本地主机(机器)名称?我使用的是PowerShell 1.0。


当前回答

在PowerShell Core v6中(适用于macOS、Linux和Windows):

[Environment]::MachineName

其他回答

类似于Powershell中的bat文件代码

Cmd

wmic path Win32_ComputerSystem get Name

电源外壳

Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name

和…

hostname.exe

主机名在Powershell中也可以正常工作

在PowerShell Core v6中(适用于macOS、Linux和Windows):

[Environment]::MachineName

你可以使用.NET Framework方法:

[系统]::GetHostName (Dns网。)

also

$ env: COMPUTERNAME

长形式:

get-content env:computername

简式:

gc env:computername