Checking Host Name Type

by Feb 6, 2009

To check whether a string contains a valid host name, you can use the CheckHostName() method provided by the System.URI type. It will return "Unknown" for any invalid host name. If you specify a valid host name, the method tells you whether it was a valid DNS name, an IPv4 or IPv6 IP address. It does not check whether the host exists.

function Check-Hostname($name) {
[System.Uri]::CheckHostName($name)
}

Check-Hostname "127.0.0.1"
Check-Hostname "2001:0:d5c7:a2ca:89e:7bd:a865:5eb6"
Check-Hostname "www.powershell.com"
Check-Hostname "///"