Jump to page sections
- PSipcalc Screenshot Example
- Download PSipcalc
- More PSipcalc Examples
- Display Network Address
- Enumerate IPs in a Network
- Find out if an IP is in a specified network
- Find out if an IP is in the enumerated collection
Originally, I was going to improve another script I wrote, to make it use runspaces for concurrency, and also implement more nmap-like features like accepting CIDR notation and enumerating subnets. I will update this article with a link to that tool if/when I get around to it. ... I did! Check out my PowerShell nmap-like program.
On 2020-02-26 I finally put the code on GitHub as well: https://github.com/EliteLoser/PSipcalc - enjoy. I might even publish a separate module. Wow.I researched a bit and found a C# library on CodePlex for performing calculations on subnets, etc., but it wouldn't load in my PowerShell session (v4, Windows 8.1). So instead, apparently, I ended up spending the weekend writing a sort of PowerShell ipcalc clone as a starting point for this other module that I mentioned above.
PSipcalc also lets you enumerate entire IP ranges supplied via CIDR notation or as IP/subnet mask, with the -Enumerate parameter. Beware that a /16 subnet takes about 10 seconds on my computer, while an /8 network took almost exactly an hour - so you will want to remember to assign to a variable or otherwise export when you run it with the -Enumerate parameter against huge IP ranges.Should work with PowerShell version 2 and up.
It supports the usual variants of syntax for providing a network: Regular CIDR notation:10.20.30.40/24
... or with a subnet mask (space or slash between works):
10.20.30.40/255.255.255.0
I also have an article about validating and listing IPv4 subnet masks here. There's an article about validating IPv4 addresses using a regex here, and one for IPv6 addresses here.
PSipcalc Screenshot Example
Download PSipcalc
Install-Module -Name PSnmap #-Scope CurrentUser -ForceChange history for PSipcalc.ps1:
- 2015-07-16: v1.2. Standardized the TotalHosts and UsableHosts properties to always be of the type int64. Formerly TotalHosts was a string, except for network lengths of 30-32, when it was an int32. UsableHosts used to be int32.
- 2015-07-15: v1.1. Added a -Contains parameter that returns a boolean value (true/false) for whether the specified IPv4 address is in the specified subnet. Based on feedback from the Reddit user "real_parbold".
*Latest wiki version of PSipcalc.ps1:
PSipcalc.ps1.txt.
*Latest wiki version of PSnmap.zip:
PSnmap.zip.
More PSipcalc Examples
Display Network Address
A little trickery to display the network address for various network lengths and the IP "10.20.30.40".
PS C:\temp> 4, 8, 16, 24, 32 | foreach { .\PSipcalc.ps1 -Net "10.20.30.40/$_" } | Select -Expand NetworkAddress
0.0.0.0
10.0.0.0
10.20.0.0
10.20.30.0
10.20.30.40
Enumerate IPs in a Network
PS C:\temp> $Results = .\PSipcalc.ps1 -NetworkAddress '192.168.0.10 255.255.255.224' -EnumeratePS C:\temp> $Results
IP : 192.168.0.10 NetworkLength : 27 SubnetMask : 255.255.255.224 NetworkAddress : 192.168.0.0 HostMin : 192.168.0.1 HostMax : 192.168.0.30 Broadcast : 192.168.0.31 UsableHosts : 30 TotalHosts : 32 IPEnumerated : {192.168.0.1, 192.168.0.2, 192.168.0.3, 192.168.0.4...} BinaryIP : 11000000101010000000000000001010 BinarySubnetMask : 11111111111111111111111111100000 BinaryNetworkAddress : 11000000101010000000000000000000 BinaryBroadcast : 11000000101010000000000000011111 PS C:\temp> $Results.IPEnumerated 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 192.168.0.5 192.168.0.6 192.168.0.7 192.168.0.8 192.168.0.9 192.168.0.10 192.168.0.11 192.168.0.12 192.168.0.13 192.168.0.14 192.168.0.15 192.168.0.16 192.168.0.17 192.168.0.18 192.168.0.19 192.168.0.20 192.168.0.21 192.168.0.22 192.168.0.23 192.168.0.24 192.168.0.25 192.168.0.26 192.168.0.27 192.168.0.28 192.168.0.29 192.168.0.30
Find out if an IP is in a specified network
If you specify multiple networks, the IP specified with -Contains will be checked against all of them, and a bool will be returned for each network.
PS C:\temp> .\PSipcalc.ps1 -Net '192.168.1.15/29' -Contains "192.168.1.10" True PS C:\temp> .\PSipcalc.ps1 -Net '192.168.1.15/29' -Contains "192.168.1.50" False
A little more trickery:
PS C:\temp> 6..17 | %{ "192.168.1.${_}: " + (.\PSipcalc.ps1 -Net '192.168.1.15/29' -Contains "192.168.1.$_") }
192.168.1.6: False
192.168.1.7: False
192.168.1.8: True
192.168.1.9: True
192.168.1.10: True
192.168.1.11: True
192.168.1.12: True
192.168.1.13: True
192.168.1.14: True
192.168.1.15: True
192.168.1.16: False
192.168.1.17: False
Find out if an IP is in the enumerated collection
PS C:\temp> $Results = .\PSipcalc.ps1 -Net '192.168.1.15/29' -Enumerate
PS C:\temp> $Results.IPEnumerated
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
# With the -contains operator
PS C:\temp> $Results.IPEnumerated -Contains '192.168.1.11'
True
# from PSv3 and up you can also use "-in" and .Contains on the array.
PS C:\temp> '192.168.1.10' -in $Results.IPEnumerated
True
PS C:\temp> $Results.IPEnumerated.Contains('192.168.1.9')
True
PS C:\temp> $Results.IPEnumerated.Contains('192.168.1.8')
False
Powershell
Windows
Networking
Subnet math
Subnet calculations
Subnet
CIDR
ipcalc
Blog articles in alphabetical order
A
- A Look at the KLP AksjeNorden Index Mutual Fund
- A primitive hex version of the seq gnu utility, written in perl
- Accessing the Bing Search API v5 using PowerShell
- Accessing the Google Custom Search API using PowerShell
- Active directory password expiration notification
- Aksje-, fonds- og ETF-utbytterapportgenerator for Nordnet-transaksjonslogg
- Ascii art characters powershell script
- Automatically delete old IIS logs with PowerShell
C
- Calculate and enumerate subnets with PSipcalc
- Calculate the trend for financial products based on close rates
- Check for open TCP ports using PowerShell
- Check if an AD user exists with Get-ADUser
- Check when servers were last patched with Windows Update via COM or WSUS
- Compiling or packaging an executable from perl code on windows
- Convert between Windows and Unix epoch with Python and Perl
- Convert file encoding using linux and iconv
- Convert from most encodings to utf8 with powershell
- ConvertTo-Json for PowerShell version 2
- Create cryptographically secure and pseudorandom data with PowerShell
- Crypto is here - and it is not going away
- Crypto logo analysis ftw
D
G
- Get rid of Psychology in the Stock Markets
- Get Folder Size with PowerShell, Blazingly Fast
- Get Linux disk space report in PowerShell
- Get-Weather cmdlet for PowerShell, using the OpenWeatherMap API
- Get-wmiobject wrapper
- Getting computer information using powershell
- Getting computer models in a domain using Powershell
- Getting computer names from AD using Powershell
- Getting usernames from active directory with powershell
- Gnu seq on steroids with hex support and descending ranges
- Gullpriser hos Gullbanken mot spotprisen til gull
H
- Have PowerShell trigger an action when CPU or memory usage reaches certain values
- Historical view of the SnP 500 Index since 1927, when corona is rampant in mid-March 2020
- How Many Bitcoins (BTC) Are Lost
- How many people own 1 full BTC
- How to check perl module version
- How to list all AD computer object properties
- Hva det innebærer at særkravet for lån til sekundærbolig bortfaller
I
L
M
P
- Parse openssl certificate date output into .NET DateTime objects
- Parse PsLoggedOn.exe Output with PowerShell
- Parse schtasks.exe Output with PowerShell
- Perl on windows
- Port scan subnets with PSnmap for PowerShell
- PowerShell Relative Strength Index (RSI) Calculator
- PowerShell .NET regex to validate IPv6 address (RFC-compliant)
- PowerShell benchmarking module built around Measure-Command
- Powershell change the wmi timeout value
- PowerShell check if file exists
- Powershell check if folder exists
- PowerShell Cmdlet for Splitting an Array
- PowerShell Executables File System Locations
- PowerShell foreach loops and ForEach-Object
- PowerShell Get-MountPointData Cmdlet
- PowerShell Java Auto-Update Script
- Powershell multi-line comments
- Powershell prompt for password convert securestring to plain text
- Powershell psexec wrapper
- PowerShell regex to accurately match IPv4 address (0-255 only)
- Powershell regular expressions
- Powershell split operator
- Powershell vs perl at text processing
- PS2CMD - embed PowerShell code in a batch file
R
- Recursively Remove Empty Folders, using PowerShell
- Remote control mom via PowerShell and TeamViewer
- Remove empty elements from an array in PowerShell
- Remove first or last n characters from a string in PowerShell
- Rename unix utility - windows port
- Renaming files using PowerShell
- Running perl one-liners and scripts from powershell
S
- Sammenlign gullpriser og sølvpriser hos norske forhandlere av edelmetall
- Self-contained batch file with perl code
- Silver - The Underrated Investment
- Simple Morningstar Fund Report Script
- Sølv - den undervurderte investeringen
- Sort a list of computers by domain first and then name, using PowerShell
- Sort strings with numbers more humanely in PowerShell
- Sorting in ascending and descending order simultaneously in PowerShell
- Spar en slant med en optimalisert kredittkortportefølje
- Spre finansiell risiko på en skattesmart måte med flere Aksjesparekontoer
- SSH from PowerShell using the SSH.NET library
- SSH-Sessions Add-on with SCP SFTP Support
- Static Mutual Fund Portfolio the Last 2 Years Up 43 Percent
- STOXR - Currency Conversion Software - Open Exchange Rates API