el2iot2.com

Howto get the hostname/machinename via the Windows command line

You can dig into your computer settings and find this in a dialog, but you can also get it from a command line interface:

hostname command

In cmd.exe:

C:>hostname
DESKTOP-MGAF23P9

In powershell:

PS C:\> hostname
DESKTOP-MGAF23P9

Environment variable

It is also available as an environment variable:

c:\>echo %computername%
DESKTOP-MGAF23P9

In powershell, echo is an alias to Write-Output so you can:

PS C:\> echo $env:computername
DESKTOP-MGAF23P9

or format as a string without the alias:

PS C:\> Write-Output "Computer Name : $env:computername"
Computer Name : DESKTOP-MGAF23P9