05 January 2015

Get all sites and subnets in AD using PowerShell

I wondered if there is a command like Get-ADSite or Get-ADSubnet in PowerShell but it turns out there isn’t.

After surfing around the net I ended up writing this oneliner:

Get-ADObject -SearchB (Get-ADRootDSE).ConfigurationNamingContext -f {objectClass -eq "site"} -Pr siteObjectBL | %{'';$_.Name;foreach($s in $_.siteObjectBL){$s}}

The output lists all sites separated by empty line. For each sites all subnets are listed.