28

Find a list of all IP addresses assigned to the local system

Hello everyone! The Brain Teaser series continues.

First, we would like to announce the winner of the previous brain teaser. We got a few answers in which many of them have a command length of 8. The shortest answer — ($x=ps), with 7 characters — was given by Mike F Robbins. So, he is the winner! In his solution, the assignment is placed inside the parenthesis so that the results of “ps” (alias to Get-Process) are stored in the variable $x as well as displayed on the console.

Congratulations Mike, you get an eBook version of Learn Windows PowerShell 3 in a Month of Lunches, Second Edition written by Don Jones and Jeffery Hicks.

Here is the new brain teaser!

Your new task is to get a list of IP addresses (IPv4 and IPv6, if present) of the local machine. Sounds easy? Well, here are some of the constraints to make it interesting:

  • You cannot use WMI
  • You cannot use IPCONFIG
  • You cannot reference $env:ComputerName or localhost as the computer name
  • Your command should return an IP address object!
  • You cannot use Windows 8 networking cmdlets or similar for other OS
Once again, the shortest answer wins! Be aware, a space is a character too.

Please use the comment box at the bottom of this page to submit your solutions by Friday. The winner will be announced on the next Monday.

Don’t have a solution of your own or has it already been posted by others? You can still participate and add your voice by voting on the existing comment by using the up/down voting arrows.

This time, the prize is again the eBook version of Microsoft Windows PowerShell 3.0 First Look written by Adam Driscoll. We would like to thank our sponsor Packt, one of the most prolific and fast-growing tech book publishers in the world, for providing such a cool prize.

Good luck!

Filed in: Brainteasers Tags: ,

2 Pingbacks/Trackbacks

28 Responses to "Find a list of all IP addresses assigned to the local system"

  1. Matt says:

    PSv3 Syntax: [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().UnicastAddresses|%{$_.Address}

  2. [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().UnicastAddresses|% Address

  3. _Emin_ says:

    systeminfo|%{@(([regex]”s{33}[d{2}]:s(?.*)”).Matches($_).Groups)[-1].Value}|%{[ipaddress]$_}

  4. [Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetUnicastAddresses().Address

  5. sahal says:

    [Net.DNS]::GetHostAddresses($h)

  6. Vinith Menon says:

    ([Net.Dns]::gethostbyname([Net.Dns]::GetHostname())).addresslist

  7. GDisk FLa says:

    [Net.Dns]::GetHostEntry(“”).AddressList

  8. sahal says:

    [Net.DNS]::GetHostAddresses([Net.DNS]::GetHostName())

  9. Sahal says:

    [Net.DNS]::GetHostAddresses(“”)

    • sahal says:

      from my two answers, i call below the finalists:

      [Net.DNS]::GetHostAddresses($h)
      [Net.DNS]::GetHostAddresses(“”)

      Note: $h doesn’t need to be defined anywhere before using.

      something interesting here, can you tell which is shorter. you may say both are equal.
      if you ask powershell it will say the one using $h is shorter, i guess because length property is not counting $ as a character.

    • I prefer this solution over one with ‘$h’ – $h *can* be defined (by accident) and you end up with an error rather than result. And length is the same:
      ‘[Net.DNS]::GetHostAddresses($h)’.Length -eq ‘[Net.DNS]::GetHostAddresses(“”)’.Length -eq 31
      True

      • sahal says:

        thank you for the feedback, very valid about using a dummy variable, it will throw error if already defined and doesn’t contain a host name.
        Another interesting point about the length property, i see you used single quote to interpret as string, and the length is same (31), but if i use double quotes ” then the one with $h says 29. why is it so? how is the Parser or length property handling this.?

        • If you double-quote it – than parser will:
          * evaluate $n to nothing (-2 chars)
          * evaluate “” to ” (that’s how you would escape double quote inside double quote to avoid terminating your string too early, -1 chars)
          If you would Invoke-Expression either, you would get an error. ;)

      • Johnny Leuthard says:

        well how about empty single quotes rather than double? It’s not smaller characters wise but it would be single rather than double so that would “shorten,” mean ‘shorten’ it. LOL

  10. _Emin_ says:

    (Test-Connection ::1)[0]|select I*

  11. Rich Leclair says:

    wouldn’t this work?

    Test-Connection ::1

    • Johnny Leuthard says:

      It’s pretty much the same thing as a ping and if you did not have IPv6 installed it would throw an error. The solution has to work for both IPv4 & v6 (“IF” v6 is present)

    • Johnny Leuthard says:

      sorry I thought one of the requirements was to not use ping. However it still is an IPv6 dependent solution.

Leave a Reply

Submit Comment

© 2014 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: