Created
November 19, 2025 21:08
-
-
Save SweetAsNZ/93889ada6129afd46324520d57e723d6 to your computer and use it in GitHub Desktop.
Expands a CIDR Range to it's constituent IP's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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