ChatGPT解决这个技术问题 Extra ChatGPT

How do I get the localhost name in PowerShell?

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


S
Strelok

You can just use the .NET Framework method:

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

also

$env:COMPUTERNAME


Note: if your DNS name is longer than 15 characters, [System.Net.Dns]::GetHostName() (doesn't truncate) is better than $env:COMPUTERNAME (truncates)
On Windows 10 Enterprise, env:COMPUTERNAME produced the following error: env:COMPUTERNAME : The term 'env:COMPUTERNAME' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + env:COMPUTERNAME + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (env:COMPUTERNAME:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
@mathisfun $env:COMPUTERNAME works fine on Windows 10 (please note dollar sign at the beginning)
$env:COMPUTERNAME won't work on Linux and macOS. However, [Environment]::MachineName does.
j
jpaugh

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

PS> hostname
KEITH1

This is a good answer, however, if you want to save the value in a variable then use the accepted answer.
You can store the value as follows $name = $(hostname)
R
RaYell

Long form:

get-content env:computername

Short form:

gc env:computername

supershort: $env:COMPUTERNAME
This will work fine, until you have a server with a name longer than 15 characters.
@GaryPendlebury what will happen then?
@Sajuuk computername is the NetBIOS name, which is limited to 15 characters.
g
grepit

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

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

G
Gary Pendlebury

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

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


f
felixfbecker

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

[Environment]::MachineName

B
BillMux

hostname also works just fine in Powershell


G
Garric

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

G
Gucu112

The most descriptive way for me is:

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

K
KentMarionVG

You can store the value as follows $name = $(hostname)

I want to add that simply executing $name = hostname will also save the localhost name of the PC into a variable.


This should be a comment.
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
T
T S

Not specifically for Powershell version 1.0 and more of an overview of different possible ways to get information via Powershell:

Native CmdLet (usually the best, but sadly really slow to get the Computername): (Get-ComputerInfo).CsDNSHostName Returns the "Name of local computer according to the domain name server" (Info about Get-ComputerInfo, Info about its return value)

Classical CommandLine executable: hostname.exe Returns the "host name portion of the full computer name of the computer" (Info about hostname.exe

Environment Variable: $env:COMPUTERNAME

Registry: (Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName).ComputerName

Static .Net Property: [Environment]::MachineName Returns "the NetBIOS name of this local computer." (Info about the System.Environment class)

Property of COM-Object (Computername via deprecated Scripting-Api): (New-Object -ComObject WScript.Network).ComputerName Returns "the name of the computer system." (Info about the WshNetwork Object, Info about the Windows Script Host Objects)

Property of WMI-Object: (Get-CimInstance -ClassName Win32_ComputerSystem).Name (Info about Win32_ComputerSystem)