List all VNet and Subnets across multiple subscriptions
Tags: azure, cloudadministration, powershell
Published: 2021-02-17 09:09:43

---
title: List all VNet and Subnets across multiple subscriptions
published: true
date: 17/02/2021 07:00:00 UTC
tags: Azure,CloudAdministration,PowerShell,PowerShell
canonical_url: https://robstr.dev/list-all-VNet-across-multiple-subscriptions/
---

It has happened to everyone, the network sprawl. You might have on-premises networks and virtual networks, maybe even in multiple clouds, and at one point you simply have lost count of your ranges and what they are used for. Usually, these ranges come from someone that is responsible for IP-ranges (preferably an IPAM solution) but what if you have a lot of teams creating VNet in a bunch of subscriptions? Well, it can get out of hand quickly.

## The script

If you are interested in learning how this script works, we’ll continue the blog post after the code. For those who just want to run the script, here you go:

```powershell
Get-AzSubscription | Foreach-Object {
    $sub = Set-AzContext -SubscriptionId $_.SubscriptionId
    $vnets = Get-AzVirtualNetwork

    foreach ($vnet in $vnets) {
        [PSCustomObject]@{
            Subscription = $sub.Subscription.Name
            Name = $vnet.Name
            Vnet = $vnet.AddressSpace.AddressPrefixes -join ', '
            Subnets = $vnet.Subnets.AddressPrefix -join ', '
        }
    }
} | Export-Csv -Delimiter ";" -Path "AzureVnet.csv"

```

This will export the results to CSV, but if you don’t want that you can remove the last pipe and the cmdlet Export-Csv.

_Note that you need to have the Az-module installed. You also have to be connected to Azure with an account that can at least read all the subscriptions and network resources._

## How the script works

We start off by getting all the subscriptions available and running them one by one through a _for each_ loop. So for every subscription, we set the active context to that subscription and populate the variable `$vnets` with all Virtual Networks in that subscription.

We run through another for each loop, where we create one new PSCustomObject per VNet in our `$vnets` variable. This is how we will represent our information, and the first couple of values makes sense. We set _Subscription_ to the name of our current subscription, and the name of the Vnet as the _Name_ field.

For our VNet address space and subnets, we could just point to the value from $vnet and be done with it. This works perfectly if you just want the results in the terminal. What I want, is to export this as a CSV so I can share this with whoever needs the list. If you try to export this value and it’s more than one, you will not get an IP range but the text `System.Collections.Generic.List`.

To get around this refer to the value we want, and use the [join operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_join?view=powershell-7) to join all the values together, separated by a comma. I also added a space after the comma to make it more readable. The VNet address space and the subnet can be multiple values, so I had to use the join operator for both of them.