Avatar of Rich Rumble
Rich Rumble
Flag for United States of America asked on

Search CIDR subnet ranges with powershell

I'd like to be able to search for an IP's within CIDR ranges/list, and output the information around that IP's CIDR file.
Very similar to this EE question https://www.experts-exchange.com/questions/29127048/Excel-CIDR-IP-Address-Overlap-Formula-Check-for-CIDR-IP-Address-Overlap.html

This is what I envision...
CIDR, Subnet_Name, Guest Network, Production, Telco, BoomChickaWowWow

Open in new window

    Found: - Guest Network

   Found: - Telco
   Found: - BoomChickaWowWow
   Not Found:

FindIp would look inside the cidr.csv and then expand the CIDR's and seqrch for matches within.
PowershellVB ScriptNetworking

Avatar of undefined
Last Comment
Rich Rumble

8/22/2022 - Mon
Aard Vark

Rather than re-invent the wheel, try this script. Just find a method to that suites you for iterating through the subnets.

function IPInRange {
        # IP Address to find.
            ([System.Net.IPAddress]$_).AddressFamily -eq 'InterNetwork'

        # Range in which to search using CIDR notation. (ippaddr/bits)
            $IP   = ($_ -split '/')[0]
            $Bits = ($_ -split '/')[1]

            (([System.Net.IPAddress]($IP)).AddressFamily -eq 'InterNetwork')

            if (-not($Bits)) {
                throw 'Missing CIDR notiation.'
            } elseif (-not(0..32 -contains [int]$Bits)) {
                throw 'Invalid CIDR notation. The valid bit range is 0 to 32.'

    # Split range into the address and the CIDR notation
    [String]$CIDRAddress = $Range.Split('/')[0]
    [int]$CIDRBits       = $Range.Split('/')[1]

    # Address from range and the search address are converted to Int32 and the full mask is calculated from the CIDR notation.
    [int]$BaseAddress    = [System.BitConverter]::ToInt32((([System.Net.IPAddress]::Parse($CIDRAddress)).GetAddressBytes()), 0)
    [int]$Address        = [System.BitConverter]::ToInt32(([System.Net.IPAddress]::Parse($IPAddress).GetAddressBytes()), 0)
    [int]$Mask           = [System.Net.IPAddress]::HostToNetworkOrder(-1 -shl ( 32 - $CIDRBits))

    # Determine whether the address is in the range.
    if (($BaseAddress -band $Mask) -eq ($Address -band $Mask)) {
    } else {

Open in new window


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rich Rumble

Wow, you guys make this look easy! oBdA is getting the majority of the points, the solution worked great! I've got a /8 with 6k different CIDR subnets/names and this searches through all of it in few seconds! I will have some follow up questions soon, keep an eye out :)
Rich Rumble

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy