Windows PowerShell Script: Resolve IP address from Hostname

In this blog post, Brian Posey highlighted Windows PowerShell as an essential Windows networking skill going forward. In a new series of blog posts, we will highlight existing networking-based Windows PowerShell scripts in the Script Center Repository on TechNet and show how they can be used for basic or advanced networking capabilities either at the Windows PowerShell command prompt or within your own custom scripts.

 

Resolve IP address from Hostname

This Windows PowerShell script in the Script Center Repository performs host name to IP address resolution through the Get-IPAddress function with the following parameters:

-HostName : A variable that defines the host name being resolved.

-IPV6only : A switch to list only the IPv6 addresses resolved for the name.

-IPV4only : A swtich to list only the IPv4 addresses resolved for the name.

You can test Get-IPAddress by copying the code into the Windows PowerShell command prompt (click Copy Code on the script page, then right-click in the Windows PowerShell command prompt to paste it in). Once entered as a function, you can use the Get-IPAddress function directly from the command line (example: PS C:\users\default> Get-IPAddress example). Alternately, paste this code into your Windows PowerShell script.

For those of you honing your PowerShell programming skills, notice how this script uses the -IPV6only and -IPV4only parameters and separate if{} sections to list the resolved IPv6 addresses, IPv4 addresses, or both.

Enjoy!

 

Joe Davies
Principal Writer
Windows Server Information Experience