Find Free Azure CIDR Blocks with PowerShell
Suppose the network team assigns 10.172.0.0/16 to an Azure landing zone. You
need to divide it into correctly aligned virtual network and subnet ranges, then find
space for future networks without overlapping anything already deployed.
This article uses PowerShell to perform both tasks: break a parent range into planned CIDR blocks and find free blocks around an existing allocation.
A small module for CIDR calculations
ipmgmt is a small open-source PowerShell module I maintain. It contains two commands:
Get-VLSMBreakdowndivides a parent network into requested subnet sizes;Get-IPRangesfinds free blocks of a requested CIDR size around occupied ranges.
Install and inspect the current version:
| |
| |
Divide an address space with VLSM
CIDR notation is less ambiguous than expressing a subnet as a number of usable addresses, so define the plan with a name and prefix length:
| |
The requested blocks are aligned and do not overlap:
| |
The command also returns the unused portions of the parent network with their Type
set to reserved. Those blocks can be kept for later allocations.
Azure usable addresses are different
The Usable property in this output is the conventional IP-network value: total
addresses minus the network and broadcast addresses. It is not the number of
addresses Azure can assign to resources.
Azure reserves five addresses in every subnet:
the first four and the last address. A /24 therefore has 251 Azure-assignable
addresses, not 254. Always apply Azure service requirements and reservations when
sizing a subnet; use the module to calculate block boundaries, not service capacity.
Find a free block
Now assume that two /24 ranges are already occupied and a new workload needs a
/22:
| |
| |
The command returns the occupied ranges with IsFree set to $false and matching
free candidates with IsFree set to $true. Selecting the first free candidate
gives us 10.172.4.0/22.
Use Azure as one source of occupied ranges
Instead of maintaining the list by hand, retrieve VNet address spaces with the Az.Network module:
| |
If you work across several subscriptions, select each context and aggregate its ranges before calculating. Azure is also not necessarily the complete source of truth: include connected on-premises networks, peered environments, reservations in an external IPAM system, and ranges assigned to work that has not been deployed yet.
Get-IPRanges only knows about the occupied ranges you pass to it.
Allocate several blocks in one run
When several new VNets are needed, add each proposed range to the occupied list before calculating the next one:
| |
| |
Adding every candidate to $occupied prevents a later iteration from returning an
overlapping block.
These results are proposals, not reservations. In a shared automation environment, two jobs can still calculate the same free range at the same time. Persist or reserve each allocation in your authoritative IPAM system before another process can claim it.
Where to go next
The same approach can feed Bicep parameters, Terraform variables, or
New-AzVirtualNetwork. The important separation is:
- collect every occupied or reserved range;
- calculate aligned, non-overlapping candidates;
- account for Azure service-specific sizing;
- reserve the selected range before deployment.
The module source, command documentation, and Pester tests are available in the ipmgmt repository.
About the Author
Andrey
Developer platforms, PowerShell, Azure, and observable systems
I am a hands-on software architect with more than 20 years of experience building developer platforms, delivery automation, and production infrastructure. I work primarily with PowerShell, C#/.NET, and Azure, turning infrastructure complexity into application-centric self-service workflows using CI/CD, GitOps, Kubernetes, infrastructure as code, and observability.
I build PowerShell tools and write about Azure automation, graph-based infrastructure analysis, messaging, and data visualization. My open-source projects include PSQuickGraph, PSGraphView, ipmgmt, and pubs.
Related Articles
Validate Azure Resource Relationships with PSRule and PowerShell Graphs
PSRule for Azure makes it straightforward to validate the properties of individual resources before deployment. But some …
Read moreHow to Toggle Logon Restrictions for AD Accounts
Written by Tino JR This script will allow an administrator to enable or disable logon restrictions for an Active Directory …
Read morePowerShell Escape Room
PowerShell Escape Room by Michiel Hamers by Michiel Hamers https://about.me/michielhamers/ Why on earth you want to create an …
Read more