PowerShell : Resolve DNS Hostname to IP and Reverse using a Single function


Use the System.Net Namespace‘s DNS ( System.Net.Dns ) class and its function ( getHostEntry() ) to resolve the Host to IPAdress resolution and Vice-Versa
NAMESPACE :  System.Net
ASSEMBLY    :  System (in System.dll)
FUNCTION     :  GetHostentry



The System.Net namespace provides a simple programming interface for many of the protocols used on networks today.
Classes in the System.Net namespace can be used to develop Windows Store apps or desktop apps. To know more on System.Net namespace and its classes, click here .

GET THE SCRIPT : Click here  to go to my GitHub gist to get the code.


Well, you can use this function in multiple ways, by default it resolves IP addresses to Host Names using the DNS query. But if you want to Resolve Host name to IP Address use the switch -HostnameToIP to do the reverse lookup

Here are some examples of its usage –

  1. You can resolve a list of IP Addresses to host namesdns1
  2. Or, you can pass a file with list of IP Addresses to get there host names resolveddns1
  3. Similarly you can pass multiple Host names and get there respective IP addresses resolved
    NOTE : Do not forget the –HostnameToIP switch for the reverse lookupdns1
  4. the function returns the Data in form of objects and you can access its attributes using Dot ( . ) operatordns1
  5. Use the -FlushDNS switch to Flush the DNS Resolver cache from your system before making the DNS querydns1

Hope you find it useful, Happy learning !

Thank you for stopping by 🙂


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