3

I want to return the short name of the domain the computer I am running on. I do not want the $env:USERDOMAIN because that is the domain the user is logged on to, which could be different to the domain the machine is in. If I do

(gwmi win32_computersystem).Domain

This gets me the FQDN, but I want something similar to return the short name (NETBIOS name?). I am currently parsing the FQDN, but I know this will lead to bugs later.

I don't have the Active Directory module installed, and company policy prevents me from installing it, so I can't simply use (Get-ADDomain).NetBIOSName from that module.

Mark Allison
  • 6,838
  • 33
  • 102
  • 151

5 Answers5

9

OK, last try.

(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1'
mjolinor
  • 66,130
  • 7
  • 114
  • 135
2

While this is old, here are the two solutions I know that seem to reliably work to get the NETBIOS name for a domain without using the AD Powershell module:

Option 1 - ADSI query. Depends on network speed for LDAP lookup, but it's not that much slower than WMI:

$Config = $RootDSE.Get("configurationNamingContext")
$netbiosName = (new-object DirectoryServices.DirectorySearcher([adsi]"LDAP://CN=Partitions,$config",'(&(objectclass=Crossref)(netBIOSName=*))')).FindOne().Properties.nETBIOSName

Option 2 - another variation on the Win32_NTDomain method given in other answers. This solves (hopefully) the problems with the empty workgroup shown on Server 2008 R2 systems and trusted domains.

In my example, the actual domain is MYTESTDOMAIN. The site name for the server is the same in both domains (deliberate design choice).

> Get-WmiObject -Class Win32_NTDomain

ClientSiteName          :
DcSiteName              :
Description             : SRV101
DnsForestName           :
DomainControllerAddress :
DomainControllerName    :
DomainName              :
Roles                   :
Status                  : Unknown

ClientSiteName          : Servers
DcSiteName              : Servers
Description             : MYTESTDOMAIN
DnsForestName           : test.my.com.au
DomainControllerAddress : \\10.0.100.100
DomainControllerName    : \\MYTESTDC003
DomainName              : MYTESTDOMAIN
Roles                   :
Status                  : OK

ClientSiteName          : Servers
DcSiteName              : Servers
Description             : MYDOMAIN
DnsForestName           : my.com.au
DomainControllerAddress : \\10.10.100.100
DomainControllerName    : \\MYDC001
DomainName              : MYDOMAIN
Roles                   :
Status                  : OK

Win32_ComputerSystem gives the computer's domain DNS name - this is unique, at least in all my tests.

> gwmi Win32_ComputerSystem

Domain              : test.my.com.au
Manufacturer        : VMware, Inc.
Model               : VMware Virtual Platform
Name                : SRV101
PrimaryOwnerName    : ICT
TotalPhysicalMemory : 12884430848

We can combine those into a nice WMI query that will only return the result for the domain that matches the Win32_ComputerSystem Domain

> Get-WmiObject -Query "SELECT * FROM Win32_NTDomain WHERE DomainName LIKE '%' AND DNSForestName = `'$((gwmi win32_computersystem).domain)`'"

ClientSiteName          : Servers
DcSiteName              : Servers
Description             : MYTESTDOMAIN
DnsForestName           : test.my.com.au
DomainControllerAddress : \\10.0.100.100
DomainControllerName    : \\MYTESTDC003
DomainName              : MYTESTDOMAIN
Roles                   :
Status                  : OK

To wrap it up in a nice one-liner that returns only the NETBIOS name:

$netbiosName = (Get-WmiObject -Query "SELECT DomainName FROM Win32_NTDomain WHERE DomainName LIKE '%' AND DNSForestName = `'$((gwmi win32_computersystem).domain)`'").DomainName

Obviously it should be tested, but it's worked in all my tests - albeit domains with only a single trust.

LeeM
  • 1,118
  • 8
  • 18
2

You should be able to achieve this without the delay inherent in the WMI object Win32_NTDomain using the ADSystemInfo COM object via this code:

$ADSystemInfo = New-Object -ComObject "ADSystemInfo"
$ADSystemInfo.GetType().InvokeMember("DomainShortName", "GetProperty", $null, $ADSystemInfo, $null)

There are other AD-related properties available from this COM object too:

https://learn.microsoft.com/en-us/windows/win32/adsi/iadsadsysteminfo-property-methods

Minkus
  • 335
  • 2
  • 13
  • This is a nice, quick solution and tested OK on all the Windows versions I have access to (including Server 2008 R2) – LeeM Jan 04 '22 at 01:47
-1

Bit late in the day for this but thought I'd offer my solution in case anyone else stumbles across this! If you just want the first part of the domain name then this code should work...

$domainName = ((gwmi Win32_ComputerSystem).Domain).Split(".")[0]
Ben01635
  • 160
  • 1
  • 3
  • 6
    That's wrong. The domain shortname is not always the first part until the dot. Exemple, you could have a domain fqdn of corpo.example.com and the domain shortname would be CORP. – Yanick Girouard May 04 '18 at 15:26
-1

As mentioned by Ben01635 this command works. The following will get you the first part of any domain. (e.g. Sub.company.com will translate to "Sub", if you wanted to get just the company part, you would change 0 to a 1, if you wanted com you would change 0 to a 3.)

(Get-WmiObject -Class win32_computersystem).domain.split(".")[0]

This command works by getting the Domaing using (Get-WmiObject -Class win32_computersystem).domain Then using the .split method to split that by the "." character. Then it selects the first part of the array that those strings are split into. This should always work to get the short path if you select 0 as it will always get the FIRST domain part that it encounters. Getting the middle Company part would not be as easy with variable domains but getting the first Short Domain form should be pretty easy. You can then go on to assign that to a Variable if you need to reference it in a script

$domainSearch = (Get-WmiObject -Class win32_computersystem).domain.split(".")[0]

You can do something similar with the OU of a computer if you are searching for Just the OU that a computer is in.

$searchOU = (([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().path).Split(",=")[3]
  • 2
    I realise this is old, but your answer isn't correct. The original q was wanting the Netbios name of the domain - your solution returns just the first part of the domain DNS name. – LeeM Jan 30 '20 at 04:43