NetBIOS domain of computer in PowerShell
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.
Thad
Microsoft MVP (Development Technologies) Senior Developer for SixPivot @DavidRGardiner
Updated on July 09, 2022Comments
-
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 about 13 yearsI 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 about 13 yearsI tried this and for mine it shows as <1E> (rather than <1B>)
-
JPBlanc about 13 yearsAccording to documentation it can be <00>, <1B>, <1C>, <1D>, <1E> so you can change the regular expression to "^ (.) *<1[BCDE]>.*$"
-
JPBlanc about 13 yearsWhat about Win32_NTDomain WMI object
-
Mike Shepard about 13 yearsThat one looks good in my environment. I like it lots better than the convoluted answer I gave. :-)
-
Thad over 12 yearsWin32_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 almost 12 yearsSorry, 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 over 11 yearsThat answer is already mentioned in my question. It doesn't return the domain of the COMPUTER though (which is what I was asking)
-
Andreas about 11 yearsThere is
USERDOMAIN
andUSERDNSDOMAIN
. The second one did the job for my task. -
Thad over 8 yearsThat looks like a varation on @Sacha's answer
-
Jez over 8 yearsYeah, it is similar. Mine just has less keystrokes :)
-
Thad over 8 yearsHa! Well I guess it is true that you only have a limited number of those to use up keysleft.com :-)
-
Jez over 8 yearsGood grief, I hope I'm still not working at the age of 90! lol
-
lesca almost 7 yearsThanks! This is the answer I am looking for.
-
Thad about 3 yearsThat looks like it's returning the user's domain, not the machine's domain
-
Thad about 3 yearsFYI 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 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.