Windows PowerShell One Liner: Name to IP Address

Windows PowerShell One Liner: Name to IP Address

Rate This
  • Comments 19

<Edited 7/2/2006 to add tags and category>
Wei Wu provided a nice one liner in response to a query in our NewsGroup: Microsoft.Public.Windows.PowerShell about how to resolve a hostname into an IP Address:

PS> [System.Net.Dns]::GetHostAddresses("www.msn.com")


IPAddressToString : 207.68.173.76
Address           : 1286423759
AddressFamily     : InterNetwork
ScopeId           :
IsIPv6Multicast   : False
IsIPv6LinkLocal   : False
IsIPv6SiteLocal   : False

 

If you want to go from address to name:

PS> [System.Net.Dns]::GetHostbyAddress("207.46.198.30")

HostName                  Aliases                  AddressList
--------                  -------                  -----------
wwwtkttest2.microsoft.com {}                       {207.46.198.30}

Jeffrey Snover
Windows PowerShell Architect

PSMDTAG:FAQ: How do I resolve a hostname?
PSMDTAG:DOTNET: GetHostByAddress

Leave a Comment
  • Please add 7 and 6 and type the answer here:
  • Post
  • Jeffrey Snover posted this on the Powershell blog
    &amp;nbsp;
    Wei Wu provided a nice one liner in response...
  • Once again PS shows it's stuff.

    The ability for PS to call into NET makes PS one hell of a powerfull tool.
  • Is there a way to create a DNS record (any type) using .net? I realize you can use WMI and the MicrosoftDNS namespace to do this however i am wanting to try out some .net via powershell and can't find a way to do it - is this possible?

    I can see from this blog that retrieving DNS information is not a problem - what about writing to DNS???

    Thanks in advance

  • PingBack from http://dmitrysotnikov.wordpress.com/2008/03/07/get-computer-by-ip-address/

  • > [System.Net.Dns]::GetHostAddresses("www.msn.com")

    From the perspective of a System Admin, this is not a great answer.  Having to track through .Net libraries to find how to get an IP suggests PowerShell thinks like a developer, and not like a command line.

    Too much typing just to get an IP; too much tracking through .Net libraries to find "how."  Admins don't just look up IPs, they have look up other types of records (NS, MX), track down why things don't resolve correctly by hitting other DNS servers....  

    Why couldn't PowerShell just make an object oriented port of NSLookup?  What's next: we have to manually create ICMP packets and monitor for a response?

  • Just what I needed - Powershell rocks!

    Still, for a seasoned Powershell user to reach the next level, a deeper understanding of the .net fw is necessary. So, this kind of post is priceless.

    Thank You so much.

  • PingBack from http://www.fuzzlinks.com/2008/07/14/powershell-team-blog-windows-powershell-one-liner-name-to-ip-address/

  • Does this work on all DNS names?

  • Great article, I was able to use it and with a minor modification do a quick reverse lookup so that we could translate a list of IPs into their respective hostname.

  • Big problem is that this .net class will check local caches and netbios.  You are not garunteed to be getting back a actual DNS record from a DNS server.

  • It will also return the IPv4 and IPv6 addresses for all enabled adapters if running on a local machine.

  • OK...I get the following result:

    Address           : 454113089

    AddressFamily     : InterNetwork

    ScopeId           :

    IsIPv6Multicast   : False

    IsIPv6LinkLocal   : False

    IsIPv6SiteLocal   : False

    IPAddressToString : 65.55.17.27

    What I want to do is use the IP address in another part of the script.  I would expect that I could use "IPAddressToString" as a property as so: "$IP.IPAddressToString" but when I do that it comes back as a null value.  How do I reference the IP address in the rest of the script after identifying it?

  • ([System.Net.Dns]::GetHostaddresses("hostname".split('.')[0]))[0].ipaddresstostring

  • ([System.Net.Dns]::GetHostaddresses("hostname".split('.')[0]))[0].ipaddresstostring

  • $hn = "hostname"

    $ip = [System.Net.Dns]::GetHostAddresses($hn) | select-object IPAddressToString -expandproperty  IPAddressToString

Page 1 of 2 (19 items) 12