Powershell – how to get short/netbios name of trusted domain

active-directorypowershell

I have a user object from Active Directory that has properties like distinguished name. I can easily get the domain portion from that, like dc=somedomain,dc=com. If it were the local domain, I could use Powershell: (get-addomain -Identity "dc=mydomain,dc=com").netbiosname to get the short name. But for this external trusted domain, that doesn't work because it just searches within the local domain. Does anyone know of another way to use Powershell to get the short name for the domain of an arbitrary AD user/group?

Best Answer

You just need to add the -Server argument to your Get-ADDomain call that specifies a DC in that forest. If you were on a non-domain joined machine, you could add -Credential to explicitly provide credentials as well. But hypothetically, your trust will pass along your current credentials automatically.

So the new command would look like this:

(Get-ADDomain 'dc=mydomain,dc=com' -Server 'dc.mydomain.com').NetBIOSName

You can also combine it with Get-ADDomainController if you don't already know a DC for the target domain.

$dc = (Get-ADDomainController -Discover -DomainName mydomain.com).HostName[0]
(Get-ADDomain 'dc=mydomain,dc=com' -Server $dc).NetBIOSName