I have a big forest with many AD Domain, and frequently add new domains. I need a list of these Ad Domain to query each DC.


GetAllTrustRelationships() is not a very failsafe way to enumerate all domains in the forest, and may include other forests/domains as well.

Grab the Domains references from the Forest object instead:

function Get-ADInside
    $Forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    $Domains = $Forest.Domains
    $Domains |Select -ExpandProperty Name
| improve this answer | |
  • this code only include the current domain, not other in the current forest. – Alban Jan 11 '16 at 15:31

this function list your Active Directory Domain avaible in the forest (include the current domain => $Forest.Domains.name)

function get-AdInside () {
    $MainForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
    $ForestTrusts = $MainForest.GetAllTrustRelationships()
    $ADs = @($MainForest.Domains.name)
    $ADs += $ForestTrusts.TargetName
    return $ADs

tested with current domain 2008-2008r2-2012+

| improve this answer | |
  • 1
    How about (Get-ADForest).Domains? – Daniel Jan 7 '16 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.