I still do some networking, although not as much as I did during my CCNP certification, and I eventually memorized subnet calculations. That, and using IP Calculators 😉 In this blog post, I will show you how you can use Chris Dent‘s PowerShell module for that.
What is IPv4 subnet math?
When using IPv4 addresses for network interfaces, Firewall settings, and other purposes, you must use subnetting. Usually, for your client, it’s just 255.255.255.0, which is a /24 subnet mask. But for smaller networks, such as those from your ISP, it can be a /29, and knowing the beginning and end IP addresses is crucial. Or when splitting larger networks into smaller segments, etc.
How to install the Indented.Net.IP module
The module is available in the PowerShell gallery, and because of that, you can use this:
Install-Module -Name Indented.Net.IP
Or, depending on whether you have Microsoft.PowerShell.PSResourceGet Module installed:
Install-PSResource -Name Indented.Net.IP
Using the module
These are the Cmdlets that are available after installing the module on your system:
| Name | Synopsis |
|---|---|
| ConvertFrom-HexIP | Converts a hexadecimal IP address into a dotted decimal string. |
| ConvertTo-BinaryIP | Converts a Decimal IP address into a binary format. |
| ConvertTo-DecimalIP | Converts a Decimal IP address into a 32-bit unsigned integer. |
| ConvertTo-DottedDecimalIP | Converts either an unsigned 32-bit integer or a dotted binary string to an IP Address. |
| ConvertTo-HexIP | Convert a dotted decimal IP address into a hexadecimal string. |
| ConvertTo-Mask | Convert a mask length to a dotted-decimal subnet mask. |
| ConvertTo-MaskLength | Convert a dotted-decimal subnet mask to a mask length. |
| ConvertTo-Subnet | Convert a start and end IP address to the closest matching subnet. |
| Get-BroadcastAddress | Get the broadcast address for a network range. |
| Get-NetworkAddress | Get the network address for a network range. |
| Get-NetworkRange | Get a list of IP addresses within the specified network. |
| Get-NetworkSummary | Generates a summary describing several properties of a network range. |
| Get-Subnet | Get a list of subnets of a given size within a defined supernet. |
| Resolve-IPAddress | Resolves an IP address expression using wildcard expressions to individual IP addresses. |
| Test-SubnetMember | Tests an IP address to determine if it falls within IP address range. |
More information here: https://github.com/indented-automation/Indented.Net.IP
Below are some examples of how you can use the module and its Cmdlets.
ConvertTo-Subnet
This is one of the times that you do most, getting the closest Subnet Mask for a specific IP range, most calculations during the CCNA exams are about getting those things right 🙂 For example, if you want an IP range within 172.16.123.10 up to 172.16.123.32… What’s the correct Subnet Mask with as many Host addresses as possible? You can use this command-line to show you that:

As you can see, when using 172.16.123.16 and a Subnet Mask of /28, you would have 14 usable addresses.
Test-SubnetMember
You can also validate whether your IP address and Subnet Mask actually correspond. For example, using the details from the chapter above, you can check if 172.16.123.17 falls in the /28 Subnet Mask or not:

The first test returned True, as expected, and the second one returned False. And that’s correct, the .12 IP address is not in the range of 172.16.123.16/28.
Get-NetworkSummary
This Cmdlet will provide you with details about the notation of the IP address, Broadcast Addresses, Class, Network in Decimal, etc. For example:

For the network 172.16.123.16/28, this will output all the information you need.
ConvertTo-MaskLength
And if you have the Subnet Mask in xxx.xxx.xx.xx form, 255.255.240.0, for example, and you need to know the Mask Length, then you can use the ConvertTo-MaskLength Cmdlet like this:

Wrapping up
And that’s how you can use Indented.Net.IP PowerShell module to do the IPv4 calculations for you 🙂 Have a lovely weekend!