Quick and easy way to resolve multiple hostnames from IP addresses using PowerShell

This time we will solve simple administrator task – we have a list of IP addresses, where we have to resolve the corresponding host names.

Very simple, you may say: I will open the command prompt and will “ping -a” or will “nslookup” it. But what will you do, if you have to resolve bulk list of hundreds, sometimes thousands IP addresses?

Let’s write a short PowerShell code! Before jumping in, to accomplish this task we will prepare simple CSV file. Simplest way to accomplish this is to create Excel table. The file will include two columns: IP and HostName. The IP column will include the addresses, the HostName column will be blank at this time (see example):

IP HostName
10.10.1.2
10.10.1.3
10.10.1.4
10.10.1.15
10.10.1.16
10.10.1.17
10.10.1.18
10.10.1.19

Save the table as “CSV (comma delimited)”. In the example, the file will be located at "C:\Temp\ips.csv".

First step will be the import of the file (the table) into the variable $ips:

$ips = Import-Csv "C:\Temp\ips.csv"

This variable is very handy. It is an array, where each member represents one line from the table, we created above, with both “fields” from the table.

Next, we will build a loop, that will go through each line of the imported file:

for ($i = 0; $i -lt $ips.Count; $i++)
{
... code goes here ...
}

For each IP, we will resolve the host name. The most convenient way for this, to implement .Net.Dns class:

$result = [System.Net.Dns]::GetHostByAddress($ips[$i].IP)

A little trick, showed below, will avoid unwanted errors to be displayed on the console:

$currentEAP = $ErrorActionPreference
$ErrorActionPreference = "silentlycontinue"
... code with possible errors ...
$ErrorActionPreference = $currentEAP

Finally, we have all needed to build the script together:

$ips = Import-Csv "C:\Temp\ips.csv"
for ($i = 0; $i -lt $ips.Count; $i++)
{
$result = $null
$currentEAP = $ErrorActionPreference
$ErrorActionPreference = "silentlycontinue"
$result = [System.Net.Dns]::GetHostByAddress($ips[$i].IP)
$ErrorActionPreference = $currentEAP
if ($result)
{
$ips[$i].HostName = [string]$result.HostName
}
Write-host $ips[$i].IP","$ips[$i].HostName
}
$ips | Export-Csv "C:\Temp\ips.csv"

You can pay attention to the last line of the code. This line exports (overwrites) the file with the final data. Now you can open the same file in Excel and enjoy the view of next:

IP HostName
10.10.1.2 host1.local
10.10.1.3 host2.local
10.10.1.4 hosttest.local
10.10.1.15 srv1.local
10.10.1.16 client1.local
10.10.1.17 printer
10.10.1.18 scanner
10.10.1.19 client2.local

You may find extra line or two in the beginning of the .CSV. This data is automatically appended by PowerShell Export-Csv cmdlet. Just omit it.

Enjoy! Do not hesitate to comment.

Advertisements

24 thoughts on “Quick and easy way to resolve multiple hostnames from IP addresses using PowerShell

  1. If the IP’s are in a range, you can replace the import with $ips = 1…255 | foreach {“10.10.1.$_}

    or some subset, and then use the array value to ping, collect data, etc.

  2. What changes would be needed if I had a list of FQDN server names and need to find the IP arrdesses for each server?

    1. You can use the next method (for example to resolve the IP of “host1.local”):

      $result = [System.Net.Dns]::GetHostAddresses(“host1.local”)
      ….

      and then invoke the first result:
      ….
      $result[0].IPAddressToString

      this will return the IP address of the host.

      Hope this helps!

  3. It is truly a nice and helpful piece of info. I am glad
    that you simply shared this useful info with us.
    Please keep us up to date like this. Thanks for sharing.

      1. Here’s my script:
        $ips=Import-Csv “C:\Users\PKLY\Desktop\R23334\SBC reconfiguration\nslookup.csv”
        for ($i = 0; $i -lt $ips.Count; $i++)
        {
        $result = $null
        $currentEAP = $ErrorActionPreference
        $ErrorActionPreference = “silentlycontinue”
        $result = [System.Net.Dns]::GetHostByAddress($ips[$i].IP)
        $ErrorActionPreference = $currentEAP
        if ($result)
        {
        $ips[$i].HostName = [string]$result.HostName
        }
        Write-host $ips[$i].IP”,”$ips[$i].HostName
        }
        $ips | Export-Csv “C:\Users\PKLY\Desktop\R23334\SBC reconfiguration\nslookup.csv”

  4. This is the right blog for anybody who hopes to find out
    about this topic. You know so much its almost hard to argue
    with you (not that I personally would want to…HaHa).
    You certainly put a brand new spin on a topic that’s been written about for years.
    Wonderful stuff, just great!

  5. Hi, if I write the code as you say, it doesn’t work, but if I write on the $result line “10.108.101.225” instead of $ips[$i].IP it works. I think may be because of the CSV file, but it seems right. Do you know what can happen?

    1. Hi, could be that you have only one data row in the CSV (except of titles)? Please try running the script on the CSV with few data rows in it. Hope this helps.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s