IPv4

Get-IPv4Network

Overview

Converts a provided network ID and CIDR prefix to an object containing the calculated network ID, broadcast address, number of IPs in the subnet and an enumerator.

Cmdlet Attributes

Copy
Copied
CmdletBinding : [DefaultParameterSetName = 'CIDR-String']
Alias         : ['gip4n']

Parameters

Network : System.String

A string representation of a subnet in CIDR notation ( ex: '192.168.255.0/24' ).

Attributes

Copy
Copied
Parameter               : [Mandatory = $true, Position = 0, ParameterSetName = 'CIDR-String', ValueFromPipeline = $true]
ValidateScriptAttribute : [{ $_.Trim() -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}' }, ErrorMessage = 'Is this a properly formatted CIDR subnet: {0}?']

NetworkID

A [ System.Net.IPAddress ] object to utilize to as the network ID.

Attributes

Copy
Copied
Parameter               : [Mandatory = $true, Position = 0, ParameterSetName = 'ID-Prefix']
ValidateScriptAttribute : [{ $_ -is [ IPAddress ] -or [ IPAddress ]::TryParse( $_, [ Ref ] $null ) }, ErrorMessage = 'Parameter "NetworkID" must be an IP address as either a System.Net.IPAddress object or string.']

PrefixLength : System.Int32

A [ System.Int32 ] number to utilize as the prefix length for subnet calculation.

Attributes

Copy
Copied
Parameter              : [Mandatory = $true, Position = 1, ParameterSetName = 'ID-Prefix']
ValidateRangeAttribute : [0, 32]

Example

Copy
Copied
PS> $net = Get-IPv4Network -Network '192.168.1.23/24' 
PS> $net.ID 
192.168.1.0 
PS> $net.Broadcast 
192.168.1.255 
PS> $net.Count 
256 
PS> $net | Foreach-Object { Write-Host $_ } 
192.168.1.1 
192.168.1.2 
... 
192.168.1.254 

History

Author Date Version Release Notes
Anthony Maxwell 07/17/2023 1.0.0 - Initial release.
Anthony Maxwell 09/01/2023 1.1.0 - Help text rewrite
- Removal of IPv4Network class
- Implementation of PSCustomObject return