Computer Name, DNS Name, and IP Address

by Mar 1, 2019

Here is a simple one-liner that returns your computers’ current IP address and its full DNS name:

 
PS> [System.Net.Dns]::GetHostEntry('')

HostName        Aliases AddressList                                
--------        ------- -----------                                
DESKTOP-7AAMJLF {}      {fe80::532:c4e:c409:b987%18, 192.168.2.129}
 

To examine a remote machine, simply submit the computer remote name instead of an empty string:

 
PS> [System.Net.Dns]::GetHostEntry('microsoft.com')

HostName      Aliases AddressList                                                   
--------      ------- -----------                                                   
microsoft.com {}      {40.113.200.201, 40.76.4.15, 104.215.148.63, 13.77.161.179...}
 

If you just would like to get the list of assigned IP addresses, try this:

 
PS> [System.Net.Dns]::GetHostEntry('microsoft.com').AddressList.IPAddressToString
40.113.200.201
40.76.4.15
104.215.148.63
13.77.161.179
40.112.72.205 
 

Your learning points:


  • The System.Net.DNS class contains a number of useful DNS-related methods. GetHostEntry() is especially useful because it accepts both computer names and IP addresses and returns the corresponding DNS names and IP addresses.
  • Submit an empty string to view your machines’ own settings


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!