.net 如何在 PowerShell 中获取本地主机名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1169891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the localhost name in PowerShell?
提问by George2
How do I get the localhost (machine) name in PowerShell? I am using PowerShell 1.0.
如何在 PowerShell 中获取本地主机(机器)名称?我正在使用 PowerShell 1.0。
回答by Strelok
You can just use the .NET Framework method:
您可以只使用 .NET Framework 方法:
[System.Net.Dns]::GetHostName()
[System.Net.Dns]::GetHostName()
also
还
$env:COMPUTERNAME
$env:COMPUTERNAME
回答by George2
Don't forget that all your old console utilities work just fine in PowerShell:
不要忘记所有旧的控制台实用程序在 PowerShell 中都可以正常工作:
PS> hostname
KEITH1
回答by RaYell
Long form:
长表:
get-content env:computername
Short form:
简写:
gc env:computername
回答by grepit
All above questions are correct but if you want the hostname and domain name try this:
以上所有问题都是正确的,但如果您想要主机名和域名,请尝试以下操作:
[System.Net.DNS]::GetHostByName('').HostName
回答by felixfbecker
In PowerShell Core v6 (works on macOS, Linux and Windows):
在 PowerShell Core v6(适用于 macOS、Linux 和 Windows)中:
[Environment]::MachineName
回答by Gary Pendlebury
A slight tweak on @CPU-100's answer, for the local FQDN:
对于本地 FQDN,对 @CPU-100 的回答稍作调整:
[System.Net.DNS]::GetHostByName($Null).HostName
[System.Net.DNS]::GetHostByName($Null).HostName
回答by Garric
An analogue of the bat file code in Powershell
类似于 Powershell 中的 bat 文件代码
Cmd
命令
wmic path Win32_ComputerSystem get Name
Powershell
电源外壳
Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty name
and ...
和 ...
hostname.exe

