Validating IP-Addresses

by Jan 30, 2009

To check for a valid IP-address, use the .NET Framework type System.Net.IPAddress and test whether the data can be converted into this format:

function isIPAddress($object) {
($object -as [System.Net.IPAddress]).IPAddressToString -eq $object -and $object -ne $null
}

isIPAddress "10"
isIPAddress "127.0.0.1"
IsIPAddress "hello"
isIPAddress $null