Networking

Convert-IpAddressToMaskLength and Convert-MaskLengthToIpAddress PowerShell functions


Convert-IpAddressToMaskLength and Convert-MaskLengthToIpAddress PowerShell functions are two supporting functions in the SB-Tools PowerShell module that do what their names suggest.

These two functions come in handy when manipulating IPv4 addresses. For example, when adding/removing IPv4 address to/from a network interface using PowerShell during automated provisioning.

For example, the Get-NetIPAddress cmdlet of the NetTCPIP module, returns information similar to

Notice that the familiar dotted decimal subnet mask is missing. Instead we get ‘PrefixLength’ which is the number of bits that represent the network address out of the 32 bit IPv4 address. So, a 24 bit prefix means a 255.255.255.0 subnet mask.

The Convert-IpAddressToMaskLength function takes one or more dotted decimal subnet masks and returns the corresponding bit lengths. For example

Convert-IpAddressToMaskLength 255.0.0.0,255.192.0.0,255.255.255.224

will return 8,10,27

Similarly, the Convert-MaskLengthToIpAddress 9,11,28 will return:


To use the SB-Tools PowerShell module which is available in the PowerShell Gallery, you need PowerShell 5. To view your PowerShell version, in an elevated PowerShell ISE window type

$PSVersionTable

To download and install the latest version of SB-Tools from the PowerShell Gallery, type

Install-Module SB-Tools,POSH-SSH -Force

SB-Tools contains functions that depend on POSH-SSH module, and they’re typically installed together.

To load the SB-Tools and POSH-SSH modules type:

Import-Module SB-Tools,POSH-SSH -DisableNameChecking

To view a list of cmdlets/functions in SB-Tools, type

Get-Command -Module SB-Tools

To view the built-in help of one of the SB-Tools functions/cmdlets, type

help <function/cmdlet name> -show

such as

help Convert-IpAddressToMaskLength -show

Advertisements