powershellactive-directorywminetbios

NetBIOS domain of computer in PowerShell


How can I get the NetBIOS (aka 'short') domain name of the current computer from PowerShell?

$ENV:USERDOMAIN displays the domain of the current user, but I want the domain that the current machine is a member of.

I've discovered you can do it pretty easily in VBScript, but apparently ADSystemInfo isn't very nice to use in PowerShell.

Update

Here's my final solution incorporating the suggestion of using Win32_NTDomain, but filtering to the current machine's domain

$wmiDomain = Get-WmiObject Win32_NTDomain -Filter "DnsForestName = '$( (Get-WmiObject Win32_ComputerSystem).Domain)'"
$domain = $wmiDomain.DomainName

Solution

  • In most cases, the default NetBIOS domain name is the leftmost label in the DNS domain name up to the first 15 bytes (NetBIOS names have a limit of 15 bytes). The NetBIOS domain name may be changed during the installation of the Active Directory, but it cannot be changed.

    The WIN32_ComputerSystem WMI object gives informations on a Windows computer

    PS C:\> Get-WmiObject Win32_ComputerSystem
    
    Domain              : WORKGROUP
    Manufacturer        : Hewlett-Packard
    Model               : HP EliteBook 8530w (XXXXXXXXX)
    Name                : ABCHPP2
    PrimaryOwnerName    : ABC
    TotalPhysicalMemory : 4190388224
    

    So the domain Name is given by :

    PS C:\> (gwmi WIN32_ComputerSystem).Domain
    

    But in domain installation, the DNS name is given. In this case, you can use nbtstat -n command to find the NetBIOS domain name which is displayed like this <DOMAIN><1B>.

    The PowerShell Command may be :

    nbtstat -n | Select-String -Pattern "^ *(.*) *<1B>.*$" | % {$_ -replace '^ *(.*) *<1B>.*$','$1'}
    

    Here is another way using WMI

    PS C:\> (gwmi Win32_NTDomain).DomainName