Resolve IP Addresses to Hostname using PowerShell


I had a previous customer shoot me an email asking for help whipping up a script to convert a list of IP addresses from a text file to their respective host names, and put that into another text file.  I put together a little demonstration script to show a way to get this done.  I am using the .Net Static Class “system.net.DNS”.  I hope this is useful to someone :)

 

# The following line read a plain list of IPs from files.  For this demo, I have

# this line commented out and added a line to just define an array of IPs here

 

#$listofIPs = Get-Content c:\IPList.txt

 

$listofIPs = "173.136.234.58","173.136.234.59","173.136.234.60"

 

#Lets create a blank array for the resolved names

$ResultList = @()

 

# Lets resolve each of these addresses

foreach ($ip in $listofIPs)

{

     $result = $null

    

     $currentEAP = $ErrorActionPreference

     $ErrorActionPreference = "silentlycontinue"

    

     #Use the DNS Static .Net class for the reverse lookup

     # details on this method found here: http://msdn.microsoft.com/en-us/library/ms143997.aspx

     $result = [System.Net.Dns]::gethostentry($ip)

    

     $ErrorActionPreference = $currentEAP

    

     If ($Result)

     {

          $Resultlist += [string]$Result.HostName

     }

     Else

     {

          $Resultlist += "$IP – No HostNameFound"

     }

}

 

# If we wanted to output the results to a text file we could do this, for this

# demo I have this line commented and another line here to echo the results to the screen

 

#$resultlist | Out-File c:\output.txt

 

$ResultList

 

 

-Gary

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.


Comments (13)

  1. dark knight says:

    Thank you for posting this. But for some reason when I use the file name option(i un-commented it out), it doesn't seem to resolve the IP address. All it does it spit back out the IP's and says "No HostnameFound" Put if I enter each IP in the script it works like a charm. Can you help?

  2. James says:

    Well done. Worked for me.

  3. Wal says:

    If the IPs are listed in your text file without quotes around them then you need to put quotes around the $ip parameter like this:

    $result = [System.Net.Dns]::gethostentry(“$ip”)

    Worked great for me too.

  4. John New says:

    Hi Gary. I took your cert class at MSFT about 2 yrs ago. How have you been?
    This was great. Exactly what I needed.

    Take care amigo.

  5. Kevin says:

    Works Great. Is there a easy way to print the ip from the iplist along hostname

  6. Manikanta says:

    Great Script

  7. Calin Iancu says:

    I used somethink like:

    $computers = "E:_scriptretea.txt"
    $hostName = "E:_scriptretea_Hname.txt"
    "" | Out-File $hostName
    Get-Content $computers | ForEach-Object {$ErrorActionPreference = "silentlycontinue";([system.net.dns]::GetHostByAddress($_)).hostname >> $hostName}

    # Where my retea.txt:
    172.16.0.10
    172.16.0.11
    172.16.0.12
    172.16.0.14
    172.16.0.16

    [tag:the] last two ip’s doesn’t exists and the script pass them $ErrorActionPreference = "silentlycontinue";

    hope that ok for you

  8. Gordon says:

    I know this is an old thread, but still useful. I made a slight modification to the script to output a file that can be imported to Excel.

    # The following line read a plain list of IPs from files. If you just want to test

    # a few, then uncomment the line to just define an array of IPs here and comment out
    # the line for the file.
    $listofIPs = Get-Content "c:IPList.txt"
    #$listofIPs = "173.136.234.58","173.136.234.59","173.136.234.60"
    #Lets create a blank array for the resolved names
    $ResultList = @()
    # Lets resolve each of these addresses
    foreach ($ip in $listofIPs)
    {
    $result = $null
    $currentEAP = $ErrorActionPreference
    $ErrorActionPreference = "silentlycontinue"

    #Use the DNS Static .Net class for the reverse lookup
    # details on this method found here:
    http://msdn.microsoft.com/en-us/library/ms143997.aspx

    $result = [System.Net.Dns]::gethostentry("$ip")

    $ErrorActionPreference = $currentEAP
    # You can comment out this next line if you don’t care to watch the results displayed on
    # on screen as you go.
    $result

    If ($Result)
    {
    $Resultlist += $IP + "," + [string]$Result.HostName
    }
    Else
    {
    $Resultlist += $IP + " , No HostNameFound"
    }
    }

    # This outputs the results to a file that you can import to Excel if you wish.
    # To just display the results on screen, comment out the Out-file line and uncomment
    # the line that contains only $Resultlist.

    $Resultlist | Out-File "c:output.txt"

    #$ResultList

  9. Ahmet says:

    This is a tiny tool for converting a list of IP Addresses to Hostname or reverse
    http://sourceforge.net/projects/iphostnameconverter/

  10. Fizz says:

    Thanks so much this worked like a champ!

  11. Bill says:

    Coud you please help me with a Powershell script that will grab the current IP address of the system, then do a lookup within DNS to match against then give me the hostname (ComputerName) to change or update the hostname of the system.

  12. mazen_daker says:

    Well done, thank you