Resolving IP Addresses (and Parameter Types, Too)

by Jan 9, 2015

PowerShell 2.0 and later

Here is a function that illustrates two things: it shows how you can limit a parameter to a given data type, and it shows how you can use a .NET method to turn IP addresses into machine names:

function Resolve-IPAddress 
    param (


By prepending the parameter $IPAddress with a type (like "IPAddress"), you leave it to PowerShell to check for invalid input.

The .NET type "System.Net.DNS" then provides helpful static methods that you can use to resolve IP addresses. Note that PowerShell does not require you to specify "System" in .NET types. You could also use the full name "System.Net.DNS" if you like.

This is what the output might look like when you use the new function Resolve-IPAddress:

PS> Resolve-IPAddress -IPAddress

HostName                     Aliases                     AddressList                
--------                     -------                     -----------                
TobiasAir1                   {}                          {}                

PS> Resolve-IPAddress -IPAddress 300.200.100.1
 Resolve-IPAddress : Cannot process argument transformation on parameter 
'IPAddress'. Cannot convert value "300.200.100.1" to type "System.Net.IPAddress". 
Error: "An invalid IP address was specified."
At line:1 char:30
+ Resolve-IPAddress -IPAddress 300.200.100.1
+                              ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Resolve-IPAddress], ParameterBindin 
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Resolve-IPAddres 

Twitter This Tip! ReTweet this Tip!