«

»

Nov 17

PowerShell: Discover Active Directory Forest Domain Controllers

Recently I needed to find all Domain Controllers in a large Active Directory forest (and see the AD Domain Functional Level for each domain).

Here’s the PowerShell code which leverages the Active Directory PowerShell module cmdlets.

 

import-module ActiveDirectory
$ADForestInfo = Get-ADForest
$ADForestInfoName = $ADForestInfo.Name
$ADForestInfoDomains = $ADForestInfo.Domains
$ADForestInfoForestMode = $ADForestInfo.ForestMode
$AllDCs = $Null
ForEach ($DomainDNS in $ADForestInfoDomains)
{
[string]$DomainDC = (Get-ADDomainController -Domain $DomainDNS -discover -service “ADWS”).HostName
$DomainInfo = Get-ADDomain -server $DomainDC
$DomainInfoDomainMode = $DomainInfo.DomainMode
[array]$AllDomainDCs = Get-ADDomainController -server $DomainDC -filter *
$AllDomainDCsCount = $AllDomainDCs.Count
Write-Output “Discovered $AllDomainDCsCount Domain Controllers in the $DomainDNS Domain (DFL: $DomainInfoDomainMode) ”
[array]$AllDCs += $AllDomainDCs
}
$AllDCsCount = $AllDCs.Count
Write-Output “Discovered $AllDCsCount Domain Controllers in the AD Forest $ADForestInfoName (FFL: $ADForestInfoForestMode) ”
$AllDCs | select domain,name,operatingsystem,OperatingSystemServicePack | sort domain | ft -auto

(Visited 1,100 times, 1 visits today)