logo

How to Collect AD Site Information Using PowerShell

A good old PowerShell will help you in case you wish to collect AD site information such as Active Directory site location, site options configured, ISTG assigned to the site, Site links and bridgehead servers. Below PowerShell Script uses New-Object function to connect to Directory Context and then get all Active Directory sites. All Active Directory sites are stored in a variable. Script uses ForEach loop to traverse through each site stored in the variable and then append the site information in a CSV file.

$ReportFile = "C:TempADSiteInfo.CSV"
Remove-item $ReportFile -ErrorAction SilentlyContinue
$ThisString="AD Site,Location,Site Option,Current ISTG,Subnets,Servers,In Site Links,Bridgehead Servers"
Add-Content "$ReportFile" $ThisString

$CurForestName = "Netwrix.com"
$a = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Forest", $CurForestName)
[array]$ADSites=[System.DirectoryServices.ActiveDirectory.Forest]::GetForest($a).sites
$ADSites
ForEach ($Site in $ADSites)
{
    $SiteName = $Site.Name
    $SiteLocation = $site.Location
    $SiteOptions = $Site.Options
    $SiteISTG = $Site.InterSiteTopologyGenerator

    [array] $SiteServers = $Site.Servers.Count
    [array] $SiteSubnets = $Site.Subnets.Count
    [array] $SiteLinks = $Site.SiteLinks.Count
    [array] $SiteBH = $Site.BridgeheadServers.Count

    $FinalVal=$SiteName+","+'"'+$SiteLocation+'"'+","+'"'+$SiteOptions+'"'+","+$SiteISTG+","+$SiteSubnets+","+$SiteServers+","+$SiteLinks+","+$SiteBH
    Add-Content "$ReportFile" $FinalVal          
}

Once above PowerShell script has been executed, you will have information for each Active Directory site stored in “C:TempADSiteInfo.CSV” file. Note that by default script connects to the current Active Directory Forest. In case you wish to change the report file location, please modify $ReportFile variable.

Need more PowerShell scripts for Active Directory? Find all the top wanted PowerShell commands for Active Directory in one blog post.

Nirmal is an MCSEx3, MCITP, and he was awarded the Microsoft MVP award in Directory Services and Windows Networking. He specializes in PowerShell Scripting, Microsoft Azure, Office 365, Directory Services, Failover Clusters, Hyper-V, and System Center products.