Export Subnets from Active Directory Sites and Services

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.

[System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

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.

$sites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

$sitesubnets = @()

foreach ($site in $sites)
{
	foreach ($subnet in $site.subnets){
	   $temp = New-Object PSCustomObject -Property @{
	   'Site' = $site.Name
	   'Subnet' = $subnet; }
	    $sitesubnets += $temp
	}
}

$sitesubnets

site-subnet1

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

$sites = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest().Sites

$sitesubnets = @()

foreach ($site in $sites)
{
	foreach ($subnet in $site.subnets){
	   $temp = New-Object PSCustomObject -Property @{
	   'Site' = $site.Name
	   'Subnet' = $subnet; }
	    $sitesubnets += $temp
	}
}

$sitesubnets | Export-CSV subnet.csv

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.

Get-ADReplicationSubnet -Filter *

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.

Get-ADReplicationSubnet -Filter * | Select-Object Name, Site

site-subnet1

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

Get-ADReplicationSubnet -Filter * | Select-Object Name, Site | Export-CSV subnets.csv

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.

10 Responses

  1. 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

    • Steve Parankewich says:

      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.

    • Dave DeRocha says:

      If you use this syntax, you can get the description as well:

      Get-ADReplicationSubnet -Filter * -Properties description

      • Roger McCarrick says:

        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”)

  2. Jagdeep Singh says:

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

    • Steve Parankewich says:

      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

  3. Chandan Singh Rathor says:

    Thumbs up … very helpful and clearly explained…. Thanks a lot for this….

  4. Bill says:

    it is very easy and simple way of getting all sites and associated subnets.Thanks

  1. October 10, 2015

    […] both Windows 7/2008 and Windows 8/2012 methods to ensure everyone is covered. Head on over to PowerShellBlogger.com for the full article. As always, leave a comment and I will be sure to […]

  2. April 4, 2018

Leave a Reply

Your email address will not be published. Required fields are marked *