PowerShell: IP and Host Name Lookups (nslookup)

When generating reports for servers, I frequently find myself needing to either find the host name of a machine from its IP or the IP from its host name. I could not find a native cmdlet to achieve this, so I began poking around MSDN and came across the DNS class in the System.Net namespace for .NET.

To lookup the IP based on hostname, try the following:

$ServerName = "WindowsPC"


To do a reverse lookup from the IP to a hostname, try the following:

$IP = “”


It is worth noting that the GetHostEntry method will work with either an IP address or a host name.  The information returned by GetHostAddress is slightly different from that returned by GetHostEntry. It is worth taking a look at both to see which will work best for you.

for more information on System.Net.DNS, take a look at the MSDN page: http://msdn.microsoft.com/en-us/library/b8hth2dy(v=VS.100).aspx




7 thoughts on “PowerShell: IP and Host Name Lookups (nslookup)

    1. Since DNS is relatively quick and 200 isn’t a huge number of addresses, a simple foreach loop would get the job done. You could either use an Excel spreadsheet (see other posts I’ve made about working with Excel in PoSH) or create an array and use export-csv to save the results. If you use the array, you may want to create a custom object to store just the name and IP.

    1. If you mean the hostname of the machine you are on, $Env:Computername should work.
      If you want to get the hostname from the IP, you can use:
      $IP = “”

  1. $List_of_ips = @(“”

    foreach ($address in $List_of_ips) {
    $hostnames += [System.Net.Dns]::GetHostEntry($address) | % {$_.HostName}
    write-host $hostnames


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s