function Test-InSubnet { param( [string]$IPAddress, [string]$CIDR ) $ip = [System.Net.IPAddress]::Parse($IPAddress) $cidrParts = $CIDR -split '/' $network = [System.Net.IPAddress]::Parse($cidrParts[0]) $prefix = [int]$cidrParts[1] $ipBytes = $ip.GetAddressBytes() [Array]::Reverse($ipBytes) $ipInt = [BitConverter]::ToUInt32($ipBytes, 0) $netBytes = $network.GetAddressBytes() [Array]::Reverse($netBytes) $netInt = [BitConverter]::ToUInt32($netBytes, 0) $mask = ([uint32]::MaxValue) -shl (32 - $prefix) -band [uint32]::MaxValue return ($ipInt -band $mask) -eq $netInt } Test-InSubnet "192.168.90.37" "192.168.90.0/24" Test-InSubnet "192.168.91.5" "192.168.90.0/23" Test-InSubnet "10.0.5.200" "10.0.4.0/23" Test-InSubnet "10.0.6.1" "10.0.4.0/23" Test-InSubnet "172.16.31.255" "172.16.16.0/20" Test-InSubnet "172.16.32.0" "172.16.16.0/20"