{"id":501,"date":"2014-11-17T15:17:55","date_gmt":"2014-11-17T20:17:55","guid":{"rendered":"http:\/\/adsecurity.org\/?p=501"},"modified":"2014-11-18T22:33:00","modified_gmt":"2014-11-19T03:33:00","slug":"powershell-discoveractive-directory-forest-domain-controllers","status":"publish","type":"post","link":"https:\/\/adsecurity.org\/?p=501","title":{"rendered":"PowerShell: Discover Active Directory Forest Domain Controllers"},"content":{"rendered":"<p>Recently I needed to find all Domain Controllers in a large Active Directory forest (and see the AD Domain Functional Level for each domain).<\/p>\n<p>Here&#8217;s the PowerShell code which leverages the Active Directory PowerShell module cmdlets.<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>import-module ActiveDirectory<br \/>\n$ADForestInfo = Get-ADForest<br \/>\n$ADForestInfoName = $ADForestInfo.Name<br \/>\n$ADForestInfoDomains = $ADForestInfo.Domains<br \/>\n$ADForestInfoForestMode = $ADForestInfo.ForestMode<br \/>\n$AllDCs = $Null<br \/>\nForEach ($DomainDNS in $ADForestInfoDomains)<br \/>\n{<br \/>\n[string]$DomainDC = (Get-ADDomainController -Domain $DomainDNS -discover -service &#8220;ADWS&#8221;).HostName<br \/>\n$DomainInfo = Get-ADDomain -server $DomainDC<br \/>\n$DomainInfoDomainMode = $DomainInfo.DomainMode<br \/>\n[array]$AllDomainDCs = Get-ADDomainController -server $DomainDC -filter *<br \/>\n$AllDomainDCsCount = $AllDomainDCs.Count<br \/>\nWrite-Output &#8220;Discovered $AllDomainDCsCount Domain Controllers in the $DomainDNS Domain (DFL: $DomainInfoDomainMode) &#8221;<br \/>\n[array]$AllDCs += $AllDomainDCs<br \/>\n}<br \/>\n$AllDCsCount = $AllDCs.Count<br \/>\nWrite-Output &#8220;Discovered $AllDCsCount Domain Controllers in the AD Forest $ADForestInfoName (FFL: $ADForestInfoForestMode) &#8221;<br \/>\n$AllDCs | select domain,name,operatingsystem,OperatingSystemServicePack | sort domain | ft -auto<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s the PowerShell code which leverages the Active Directory PowerShell module cmdlets. &nbsp; import-module ActiveDirectory $ADForestInfo = Get-ADForest $ADForestInfoName = $ADForestInfo.Name $ADForestInfoDomains = $ADForestInfo.Domains $ADForestInfoForestMode = $ADForestInfo.ForestMode $AllDCs = $Null ForEach &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/adsecurity.org\/?p=501\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[20,264,262,263,22],"class_list":["post-501","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-activedirectory","tag-domainfunctionallevel","tag-forestdomaincontrollers","tag-powershellad","tag-powershellcode","item-wrap"],"_links":{"self":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=501"}],"version-history":[{"count":2,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/501\/revisions"}],"predecessor-version":[{"id":537,"href":"https:\/\/adsecurity.org\/index.php?rest_route=\/wp\/v2\/posts\/501\/revisions\/537"}],"wp:attachment":[{"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adsecurity.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}