NetBIOS domain of computer in PowerShell

72,562

Solution 1

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

Solution 2

Use env: to get environment settings through PowerShell

NetBIOS: $env:userdomain

FQDN: $env:userdnsdomain

To see all the values:

dir env:  (no $)

Solution 3

import-module activedirectory
(Get-ADDomain -Identity (Get-WmiObject Win32_ComputerSystem).Domain).NetBIOSName

Solution 4

From Here

# Retrieve Distinguished Name of current domain.
$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Root = $Domain.GetDirectoryEntry()
$Base = ($Root.distinguishedName)

# Use the NameTranslate object.
$objTrans = New-Object -comObject "NameTranslate"
$objNT = $objTrans.GetType()

# Invoke the Init method to Initialize NameTranslate by locating
# the Global Catalog. Note the constant 3 is ADS_NAME_INITTYPE_GC.
$objNT.InvokeMember("Init", "InvokeMethod", $Null, $objTrans, (3, $Null))

# Use the Set method to specify the Distinguished Name of the current domain.
# Note the constant 1 is ADS_NAME_TYPE_1779.
$objNT.InvokeMember("Set", "InvokeMethod", $Null, $objTrans, (1, "$Base"))

# Use the Get method to retrieve the NetBIOS name of the current domain.
# Note the constant 3 is ADS_NAME_TYPE_NT4.
# The value retrieved includes a trailing backslash.
$strDomain = $objNT.InvokeMember("Get", "InvokeMethod", $Null, $objTrans, 3)

Solution 5

OP is after "computer domain" so the answer would be $GetComputerDomain (below) but I will add the $GetUserDomain also for reference.

$GetComputerDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()).Name
$GetUserDomain = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).Name

I find the wmi (gwmi) option to be extremely slow, especially, when you are querying the Win32_NTDomain class. I have a multi-trusted domain environment and it takes forever when I just need that simple info quick.

Share:
72,562
Thad
Author by

Thad

Microsoft MVP (Development Technologies) Senior Developer for SixPivot @DavidRGardiner

Updated on July 09, 2022

Comments

  • Thad
    Thad almost 2 years

    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
    
  • Thad
    Thad about 13 years
    I already tried that approach and it doesn't work. The 'Domain' property is not the short/NetBIOS name that I'm after. That contains the full AD domain name
  • Mike Shepard
    Mike Shepard about 13 years
    I tried this and for mine it shows as <1E> (rather than <1B>)
  • JPBlanc
    JPBlanc about 13 years
    According to documentation it can be <00>, <1B>, <1C>, <1D>, <1E> so you can change the regular expression to "^ (.) *<1[BCDE]>.*$"
  • JPBlanc
    JPBlanc about 13 years
    What about Win32_NTDomain WMI object
  • Mike Shepard
    Mike Shepard about 13 years
    That one looks good in my environment. I like it lots better than the convoluted answer I gave. :-)
  • Thad
    Thad over 12 years
    Win32_NTDomain looks like a winner. Note that returns multiple entries, including one for the local computer and probably other trusted domains (if you have any)
  • Thad
    Thad almost 12 years
    Sorry, that's not the results I was after. Those both return a full DNS-style domain name. I was after the short/NetBIOS-style domain name.
  • Thad
    Thad over 11 years
    That answer is already mentioned in my question. It doesn't return the domain of the COMPUTER though (which is what I was asking)
  • Andreas
    Andreas about 11 years
    There is USERDOMAIN and USERDNSDOMAIN. The second one did the job for my task.
  • Thad
    Thad over 8 years
    That looks like a varation on @Sacha's answer
  • Jez
    Jez over 8 years
    Yeah, it is similar. Mine just has less keystrokes :)
  • Thad
    Thad over 8 years
    Ha! Well I guess it is true that you only have a limited number of those to use up keysleft.com :-)
  • Jez
    Jez over 8 years
    Good grief, I hope I'm still not working at the age of 90! lol
  • lesca
    lesca almost 7 years
    Thanks! This is the answer I am looking for.
  • Thad
    Thad about 3 years
    That looks like it's returning the user's domain, not the machine's domain
  • Thad
    Thad about 3 years
    FYI I tried this on Windows PowerShell on Windows 10 20H2 and the first line fails with Exception calling "GetCurrentDomain" with "0" argument(s): "Current security context is not associated with an Active Directory domain or forest."
  • Minkus
    Minkus about 3 years
    @DavidGardiner Have checked today & the WinNTSystemInfo's DomainName property does return the user's domain, not the machine's domain. But the ADSystemInfo DomainShortName property is as you wanted it to be. I've updated the answer to reflect this.