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

  • http://twitter.com/mattifestation Matt

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

  • http://mikefrobbins.com/ Mike F Robbins

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

    • sahal

      Cool one, i couldn’t think more short.

    • sahal

      cool one, cant think shorter than this

  • http://twitter.com/p0w3rsh3ll _Emin_


    • http://twitter.com/p0w3rsh3ll _Emin_

      see image for missing characters between ? and .

    • Johnny Leuthard

      Although not the shortest I like the use of a regular expression.

  • http://mikefrobbins.com/ Mike F Robbins


  • sahal


    • sahal

      This is a possible solution as the brainteaser doesn’t say “All” IP list, as this lists only active IPs :-)

    • manas

      hmmm good one

  • Vinith Menon


  • GDisk FLa


  • sahal


  • Sahal


    • sahal

      from my two answers, i call below the finalists:


      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.

    • http://twitter.com/bielawb Bartek Bielawski

      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

      • sahal

        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.?

        • http://twitter.com/bielawb Bartek Bielawski

          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

        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

  • http://twitter.com/p0w3rsh3ll _Emin_

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

    • Johnny Leuthard

      That’s an IPv6 address if IPv6 is not installed you will get an error.

  • http://twitter.com/Rich_Leclair Rich Leclair

    wouldn’t this work?

    Test-Connection ::1

    • Johnny Leuthard

      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

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

  • Pingback: $(Get-Exchange).Info() » PSMag PowerShell Brain Teaser: finding list of active IP Addresses of Local Machine

  • Pingback: PowerShellでIPアドレスやHOSTNAMEを取得 « guitarrapc.wordpress.com

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