2

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.

2 Answers 2

1

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
}
1
  • this code only include the current domain, not other in the current forest.
    – Alban
    Jan 11, 2016 at 15:31
0

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+

1
  • 1
    How about (Get-ADForest).Domains?
    – Daniel
    Jan 7, 2016 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .