Search code examples
powershelladsi

What's the API to get the top domain (root domain) from any domain in the hierarchy like tree or child domain?


What's the API to get the top domain (root domain) from any domain in the hierarchy like tree or child domain?

The closest I could find were GetForest or GetCurrentForest but that's not what I am looking for..

[System.DirectoryServices.ActiveDirectory.Forest]::GetForest(directorycontext)

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()

Thanks in advance, -SunMan


Solution

  • To get from a foreign domain name to its forest root domain, use Domain.GetDomain() first, then grab the root domain object through the Forest property:

    $ForeignDomainDNS = 'other.domain.tld'
    $ForeignDomainCtx = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext 'Domain',$ForeignDomain
    $ForeignDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($ForeignDomainCtx)
    $ForeignRootDomain = $ForeignDomain.Forest.RootDomain
    

    $ForeignRootDomain now contains the Domain object representing the root domain of the foreign domain's forest