Export Subnets from Active Directory Sites and Services

Active Directory, Windows

I was asked if I could produce all the subnets from our environment. I was quick to turn to PowerShell for this request. As long as you have all of your subnets properly associated to sites in Active Directory, PowerShell can be utilized to get you the subnets. The commands to grab this information depend on the operating system you are running so I provided both methods.

If you are using Windows 7/2008 you can use the following method:

Step #1: The following command produces a list of all the Active Directory sites and their details.

site-subnet1

Step #2: Now we just need to assign the command a variable allowing us to select and extract the subnets from it’s properties. We utilize a custom object and a foreach loop that will add each subnet and site name to the list.

site-subnet1

Step #3: If you would like to export this information to a spreadsheet simply add an Export-CSV to the pipeline.

If you are running Windows 8/Windows Server 2012 life is a bit easier as you can use the built in Active Directory commandlets for managing Active Directory sites and services. Simply install the Active Directory modules or run them from the domain controller with the following commandlets. You will have to ensure your Domain Controller is running Active Directory Web Services if you plan on running these remotely.

Step #1: The following command returns all the subnets in Active Directory Sites and Services.

site-subnet1

Step #2: Now we simply have to select the appropriate properties that we want to include. In this case I chose the Name of the subnet and the Site.

site-subnet1

Step #3: If you would like to export this information to a spreadsheet simply add an Export-CSV to the pipeline.

I hope you ran across this and found it useful. Feel free to leave a comment with your problem or need and I will update the post with the solution.

8 comments

  • Hi, thanks for the scripts.
    I see that you can get the “Location” information for each subnet, but can you get “Description”? In AD Sites and Services, when you right click a subnet, there is a field for Description. I want to capture that. I tried $subnet.description which didnt work. $subnet.location does capture the Location info.

    Thanks

    • I worked on this for a bit, trying to export the PROPERTIES but was unable to extract that Description through PowerShell. I will have to dig deeper.

      • Thanks very much. I got your reply in email yesterday, Dec 20 2016, a month after you posted it. But anyway it works and thanks a million. In my script, I use your solution as follows:

        foreach ($subnet in $site.subnets ) {
        $sites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

        #Get the domain name
        $ADDomain=Get-ADDomain
        $DomName=$ADDomain.DNSRoot
        $sitesubnets = @()

        foreach ($site in $sites)
        {
        foreach ($subnet in $site.subnets ) {

        $temp = New-Object PSCustomObject -Property @{
        ‘Site’ = $site.Name
        ‘Subnet’ = $subnet;
        ‘Description’ = (Get-ADReplicationSubnet “$subnet” -Properties description).Description }

        $sitesubnets += $temp }
        }

        #Export to CSV file, name based on Domain Name.
        $sitesubnets | Export-CSV (“C:\scripts\” + $DomName + “-subnets.csv”)

  • What modification would be required to get the list of subnets for sites in a forest that is not my current one?

    • Instead of using [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites you can use the following to specify the different forest:

      $context = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext(‘Forest’,’other.forest.name’)
      [System.DirectoryServices.ActiveDirectory.Forest]::GetForest($context).Sites

Leave a Reply