George2
George2

Reputation: 45771

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.

Upvotes: 218

Views: 281041

Answers (13)

Gary Pendlebury
Gary Pendlebury

Reputation: 375

A slight tweak on @grepit's answer, for the local FQDN:

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

Upvotes: 9

Marcello Miorelli
Marcello Miorelli

Reputation: 3678

I loved the code posted by T S above, and the links and explanations he posted, very thankful.

I got his code and wrapped into a function and cut all the other output, just to output the computer name. in my case this is very handy as it tells me which domain my computer lives in.

function Get-FullyQualifiedComputerName {
    Add-Type -TypeDefinition @'
    public enum COMPUTER_NAME_FORMAT{
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified,
        ComputerNameMax,
    }
    public static class Kernel32{
        [System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, System.Text.StringBuilder lpBuffer, ref uint lpnSize);
    }
'@ | Out-Null

    $len = 0
    [Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $null, [ref]$len) | Out-Null
    $sb = [System.Text.StringBuilder]::new([int]$len)
    $len = $sb.Capacity
    [Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $sb, [ref]$len) | Out-Null
    return $sb.ToString().Trim()
}

# Example of how to use it:


$fullyQualifiedComputerName = Get-FullyQualifiedComputerName
Write-Output $fullyQualifiedComputerName

enter image description here

Upvotes: 0

GottHold
GottHold

Reputation: 49

if you want the hostname don't user $env:computername . I had just the case where $env:computername cut off the last letter where as hostname would return to full name. enter image description here

Upvotes: 2

T S
T S

Reputation: 1885

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

Add-Type -TypeDefinition @'
    public enum COMPUTER_NAME_FORMAT{
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified,
        ComputerNameMax,
    }
    public static class Kernel32{
        [System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, System.Text.StringBuilder lpBuffer, ref uint lpnSize);
    }
'@
$len = 0
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $null, [ref]$len); #get required size
$sb = [System.Text.StringBuilder]::new([int]$len) #create StringBuilder with required capacity (ensure int constructor is used, otherwise powershell chooses string constructor for uint value...)
$len = $sb.Capacity #get actual capacity of StringBuilder (important, as maybe StringBuilder was constructed differently than expected)
[Kernel32]::GetComputerNameEx([COMPUTER_NAME_FORMAT]::ComputerNameDnsFullyQualified, $sb, [ref]$len);
$sb.ToString()

Returns "The fully qualified DNS name that uniquely identifies the local computer. This name is a combination of the DNS host name and the DNS domain name, using the form HostName.DomainName. If the local computer is a node in a cluster, lpBuffer receives the fully qualified DNS name of the cluster virtual server."
(Info about the GetComputerNameEx API function, Info about the COMPUTER_NAME_FORMAT enumeration, used c# signature of COMPUTER_NAME_FORMAT (unofficial), used c# signature of GetComputerNameEx (unofficial), short blogpost about P/Invoke in powershell)
Note: Use P/Invoke with caution. Sufficiently incorrect usage can actually make powershell crash. The Add-Type call is somewhat slow (but only needs to be called once in the script).

Upvotes: 7

KentMarion
KentMarion

Reputation: 35

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.

Upvotes: 1

Gucu112
Gucu112

Reputation: 957

The most descriptive way for me is:

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

Upvotes: 2

BillMux
BillMux

Reputation: 83

hostname also works just fine in Powershell

Upvotes: 5

Garric
Garric

Reputation: 724

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

Upvotes: 4

felixfbecker
felixfbecker

Reputation: 2363

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

[Environment]::MachineName

Upvotes: 10

grepit
grepit

Reputation: 22382

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

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

Upvotes: 18

Keith Hill
Keith Hill

Reputation:

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

PS> hostname
KEITH1

Upvotes: 73

RaYell
RaYell

Reputation: 70414

Long form:

get-content env:computername

Short form:

gc env:computername

Upvotes: 40

Strelok
Strelok

Reputation: 51441

You can just use the .NET Framework method:

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

also

$env:COMPUTERNAME

Upvotes: 309

Related Questions