Resolving IP to Hostname with PowerShell

Posted On 2016-01-15 by dwirch
Tags: Scripting Powershell Tip 
Views: 7951

Getting the hostname from an IP address (or vice versa) is no great magic, and can be done easily with PowerShell, by simply leveraging .Net to do the work.

We've all done something like this to get resolve an IP address:


Or this to get an IP address from a hostname:


But did you ever notice that sea of red when you specify an invalid IP address or hostname? When you do that, you'll get a big,red error message similar to the following:

Exception calling "GetHostEntry" with "1" argument(s): "No such host is known"
At line:1 char:1
+ [System.Net.Dns]::gethostentry("").HostName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SocketException

I recently got asked how that nonsense could be avoided. Well, the answer is to simply encapsulate the process in a function that wraps up the .Net method to get the data back. It's pretty simple, and here is the code:

# noise if we fail

$ErrorActionPreference = "silentlycontinue"
$Result = $null

# Pass the IP to .Net for name resolution.

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

# process the results

If ($Result)
$MyResult = [string]$Result.HostName
$MyResult = "unresolved"

# Send it to the output


Alternatively, you could also plug this directly into a standalone script. For example, lets say that you have a list of IP addresses in a file that you want to resolve to host names. No problem: # Get list from file, initialize empty array

$ListOfIPs=Get-content ".\hosts.txt"
$ResultList = @()

# roll through the list, resolving as we
# go with the .Net DNS resolver

foreach ($IP in $ListOfIPs)
# We don't want to see any errors as we go, right?

$ErrorActionPreference = "silentlycontinue"
$Result = $null

# status to user, just so they know that something
# is still happening, then pass the current IP
# to .Net for name resolution.

write-host "resolving $IP"
$result = [System.Net.Dns]::gethostentry($IP)

# Enter into the array, with the returned results.

If ($Result)
$ResultList += "$IP," + [string]$Result.HostName
$ResultList += "$IP,unresolved"

# send it out to a file, and inform the user we are done

$ResultList | Out-File .\resolved.txt
write-host "name resolution complete"

Again, pretty straightforward.

First, it grabs the list of IP addresses from a file called hosts.txt. Next, the script runs through the list, resolving the IP addresses (if it can), storing the results in $ResultList. Finally, the output is sent to another file called resolved.txt

I hope this is useful to someone, and drop a note in the comments or in the forums if you have questions.

About the Author

has posted a total of 192 articles.

Comments On This Post

By: AnonymousCoward
Date: 2016-08-29

Have all my upvotes. Reposting this to reddit. This was nearly impossible to find for servers still on powershell 2.0. Nice work

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links