Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – IPv4 Range

Posted on March 10, 2016March 10, 2016 By David Kittell

This first function is helpful to provide a range of IPv4 addresses within a set range

function New-IPRange ($start, $end)
    {
        # created by Dr. Tobias Weltner, MVP PowerShell
        $ip1 = ([System.Net.IPAddress]$start).GetAddressBytes()
        [Array]::Reverse($ip1)
        $ip1 = ([System.Net.IPAddress]($ip1 -join '.')).Address
        $ip2 = ([System.Net.IPAddress]$end).GetAddressBytes()
        [Array]::Reverse($ip2)
        $ip2 = ([System.Net.IPAddress]($ip2 -join '.')).Address
 
        for ($x=$ip1; $x -le $ip2; $x++)
            {
                $ip = ([System.Net.IPAddress]$x).GetAddressBytes()
                [Array]::Reverse($ip)
                $ip -join '.'
            }
    }
New-IPRange 192.168.10.10 192.168.10.20
New IP Range
192.168.10.10
192.168.10.11
192.168.10.12
192.168.10.13
192.168.10.14
192.168.10.15
192.168.10.16
192.168.10.17
192.168.10.18
192.168.10.19
192.168.10.20

This next function will get the broadcast IPv4 address from a CIDR range

function Get-Broadcast ($addressAndCidr)
    {
        $addressAndCidr = $addressAndCidr.Split("/")
        $addressInBin = (New-IPv4toBin $addressAndCidr[0]).ToCharArray()
        for($i=0;$i -lt $addressInBin.length;$i++)
            {
                if($i -ge $addressAndCidr[1])
                    {
                        $addressInBin[$i] = "1"
                    } 
            }
        [string[]]$addressInInt32 = @()
        for ($i = 0;$i -lt $addressInBin.length;$i++)
            {
                $partAddressInBin += $addressInBin[$i] 
                if(($i+1)%8 -eq 0)
                    {
                        $partAddressInBin = $partAddressInBin -join ""
                        $addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2)
                        $partAddressInBin = ""
                    }
            }
        $addressInInt32 = $addressInInt32 -join "."
        return $addressInInt32
    }
Get-Broadcast 192.168.10.10/27
New IP Range Broadcast
192.168.10.31

Next this function will detect if a specified IPv4 address is in the range

function Test-IPinIPRange ($Address,$Lower,$Mask)
    {
        [Char[]]$a = (New-IPv4toBin $Lower).ToCharArray()
        if($mask -like "*.*")
            {
                [Char[]]$b = (New-IPv4toBin $Mask).ToCharArray()
            }
        else
            {
                [Int[]]$array = (1..32)
                for($i=0;$i -lt $array.length;$i++)
                    {
                        if($array[$i] -gt $mask){$array[$i]="0"}else{$array[$i]="1"}
                    }
                [string]$mask = $array -join ""
                [Char[]]$b = $mask.ToCharArray()
            }
        [Char[]]$c = (New-IPv4toBin $Address).ToCharArray()
        $res = $true
        for($i=0;$i -le $a.length;$i++)
            {
                if($a[$i] -ne $c[$i] -and $b[$i] -ne "0")
                    {
                        $res = $false
                    } 
            }
        return $res
    }
Write-Output "`r`nTest If IP In Range - 192.168.23.128/25"
Test-IPinIPRange "192.168.23.200" "192.168.23.12" "255.255.255.128"
Write-Output "`r`nTest If IP In Range - 192.168.23.127/24"
Test-IPinIPRange "192.168.23.127" "192.168.23.12" "24"
Test If IP In Range - 192.168.23.128/25
False

Test If IP In Range - 192.168.23.127/24
True

This next function will convert an IPv4 address to a Bin

function New-IPv4toBin ($ipv4)
    {
        $BinNum = $ipv4 -split '\.' | ForEach-Object {[System.Convert]::ToString($_,2).PadLeft(8,'0')}
        return $binNum -join ""
    }
Write-Output "`r`nIP To Bin"
New-IPv4toBin 192.168.10.10
IP To Bin
11000000101010000000101000001010

This last function will convert a Bin to an IPv4 address

function New-IPv4fromBin($addressInBin)
    {
        [string[]]$addressInInt32 = @()
        $addressInBin = $addressInBin.ToCharArray()
        for ($i = 0;$i -lt $addressInBin.length;$i++)
            {
                $partAddressInBin += $addressInBin[$i]
                if(($i+1)%8 -eq 0)
                    {
                        $partAddressInBin = $partAddressInBin -join ""
                        $addressInInt32 += [Convert]::ToInt32($partAddressInBin -join "",2)
                        $partAddressInBin = ""
                    }
            }
	    $addressInInt32 = $addressInInt32 -join "."
	    return $addressInInt32
    }
Write-Output "`r`nIP From Bin - 192.168.23.250"
New-IPv4fromBin "11000000101010000001011111111010"

Write-Output "`r`nIP From Bin - 192.168.10.10"
New-IPv4fromBin "11000000101010000000101000001010"
IP From Bin - 192.168.23.250
192.168.23.250

IP From Bin - 192.168.10.10
192.168.10.10
Write-Output "`r`nIP CIDR to Range"
New-IPRange "192.168.23.120" (Get-Broadcast "192.168.23.120/25")
IP CIDR to Range
192.168.23.120
192.168.23.121
192.168.23.122
192.168.23.123
192.168.23.124
192.168.23.125
192.168.23.126
192.168.23.127

References: http://ficility.net/2013/03/16/powershell-example-how-to-work-with-the-ip-addresses-ipv4/

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell CIDRCIDR CalculationIP AddressIP ConversionIPv4

Post navigation

Previous post
Next post

Related Posts

MSSQL – Remove Line Breaks

Posted on December 15, 2014October 26, 2015

Sometimes you need to remove line breaks or carriage returns, the code below should help. SELECT REPLACE(REPLACE([VarChar Column], CHAR(13), ‘ ‘), CHAR(10), ‘ ‘) The above script will not work if the column type is TEXT so you must convert it first. SELECT REPLACE(REPLACE(CAST([TEXT Column] AS VarChar(Max)), CHAR(13), ‘ ‘),…

Read More
Code

Ubuntu – WordPress Install

Posted on September 2, 2015May 31, 2017

These steps are based on Ubuntu 14.10 Server 64-bit but can be applied to virtually any Debian based UNIX distribution. Originally Posted on September 2, 2015Last Updated on May 31, 2017 All information on this site is shared with the intention to help. Before any source code or program is…

Read More

Create Shortcut (VB, VB.Net, PowerShell)

Posted on December 14, 2013December 10, 2019

Sub Create_ShortCut(ByVal TargetPath As String, ByVal ShortCutPath As String, ByVal ShortCutname As String, Optional ByVal WorkPath As String = "", Optional ByVal Window_Style As Short = 0, Optional ByVal IconNum As Short = 0) Dim VbsObj As Object VbsObj = CreateObject("WScript.Shell") Dim MyShortcut As Object ShortCutPath = ShortCutPath MyShortcut =…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes