¿Cómo obtengo el nombre localhost en PowerShell?


¿Cómo obtengo el nombre localhost (máquina) en PowerShell? Estoy usando PowerShell 1.0.

Author: Ryan Gates, 2009-07-23

5 answers

Puede usar el método. NET Framework:

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

También

$env:COMPUTERNAME

 215
Author: Strelok,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-23 06:11:25

No olvide que todas sus antiguas utilidades de consola funcionan bien en PowerShell:

PS> hostname
KEITH1
 54
Author: jpaugh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-01 16:48:42

Forma larga:

get-content env:computername

Forma corta:

gc env:computername
 33
Author: RaYell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-23 06:12:05

Todas las preguntas anteriores son correctas, pero si desea el nombre de host y el nombre de dominio, intente esto:

 [System.Net.DNS]::GetHostByName('').HostName
 7
Author: CPU 100,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-24 21:07:48

Un pequeño ajuste en la respuesta de @CPU-100, para el FQDN local:

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

 0
Author: Gary Pendlebury,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-07 09:39:31