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.
Install-Module -Name PSnmap #-Scope CurrentUser -ForceChange history for PSipcalc.ps1:
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
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
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
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') FalsePowershell
Minimum cookies is the standard setting. This website uses Google Analytics and Google Ads, and these products may set cookies. By continuing to use this website, you accept this.