How to retrieve Azure subnets details configured under VNET across multiple resource groups

Sufiyan Shaikh 20 Reputation points
2023-11-09T11:05:25.16+00:00

Hello,

please help to know how to retrieve Azure subnets details configured under VNET across multiple resource groups using Powershell command.

Regards,

Sufiyan Shaikh

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,507 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tushar Kumar 3,331 Reputation points MVP
    2023-11-09T12:25:12.8933333+00:00

    Hi Sufiyan Shaikh,

    Thank you for posting your query in Microsoft QnA !

    You can use below script to get all the Subnet details :

    Connect-AzAccount 
    # Get all virtual networks in the subscription
    $vnetList = Get-AzVirtualNetwork
    
    foreach ($vnet in $vnetList) {
        Write-Host "Virtual Network: $($vnet.Name), Resource Group: $($vnet.ResourceGroupName)"
    
        # Get all subnets in the virtual network
        $subnetList = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet
    
        foreach ($subnet in $subnetList) {
            Write-Host "  Subnet Name: $($subnet.Name), Address Prefix: $($subnet.AddressPrefix)"
        }
    
        Write-Host ""
    }
    
    
    

    You can modify the same to achieve your specific use case.

    Please click " accept as answer " if this helps.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Luis Arias 7,121 Reputation points
    2023-11-09T12:11:26.6966667+00:00

    Hi Sufiyan,

    You can use this code to get all subnets on your current subscription , Easy way:

    $vnet = Get-AzVirtualNetwork
    $vnet.Subnets | Format-List
    

    Additionally This alternative code get the subnets Ids and others details modifying the $network.[Information that you need] , also you can update if you want for specific resource with a filter when you get vnets:

    $vnet = Get-AzVirtualNetwork
    
    foreach ($network in $vnet)
    {
        $subnets = $network.Subnets
        foreach ($subnet in $subnets)
        {
            Write-Output $subnet.Id
        }
    }
    
    

    Cheers,

    Luis Arias


    If the information helped address your question, please Accept the answer.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.