Skip to content

Instantly share code, notes, and snippets.

@SweetAsNZ
Created November 19, 2025 21:08
Show Gist options
  • Select an option

  • Save SweetAsNZ/93889ada6129afd46324520d57e723d6 to your computer and use it in GitHub Desktop.

Select an option

Save SweetAsNZ/93889ada6129afd46324520d57e723d6 to your computer and use it in GitHub Desktop.
Expands a CIDR Range to it's constituent IP's
function Get-CIDRRange {
<#
.SYNOPSIS
Expands a CIDR Range
.DESCRIPTION
Expands a CIDR Range to it's constituent IP's
.EXAMPLE
Get-CIDRRange -CIDR "192.168.1.0/24"
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$CIDR
)
$parts = $CIDR -split "/"
$ip = $parts[0]
$prefix = $parts[1]
$ipParts = $ip -split "\."
$ipInt = 0
for ($i = 0; $i -lt 4; $i++) {
$ipInt = $ipInt -bor ($ipParts[$i] -shl (24 - $i * 8))
}
$mask = ([Math]::Pow(2, 32) - 1) -shl (32 - $prefix)
$start = $ipInt -band $mask
$end = $start -bor (-bnot $mask)
for ($i = $start; $i -le $end; $i++) {
$ip = [System.Net.IPAddress]::Parse($i)
$ip.IPAddressToString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment