Get the local IP Address(es)

Last year there was a brain teaser organised by Powershell Magazine.
The challenge consisted in finding a list of all IP addresses assigned to the local system

How many ways exist to retrieve the IP Addresses ?

Well it depends on the version of Powershell that you use and on what system you are:

If you’re on Powershell Version 3 on Windows 8 or version 4.0 on Windows 8.1, you can use the new CIM cmdlets:

(Get-NetIPConfiguration).IPv4Address

or

(Get-NetIPAddress).IPAddress

or by using the alias of Get-NetIPConfiguration

(gip).IPv4Address

There are also more “traditional” ways of getting this info by using

  • WMI
  • (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').IPAddress

    …shortened to in V3:

    (gwmi Win32_NetworkAdapterConfiguration|? ipe*).IPAddress

    …and shortened to in V2:

    (gwmi Win32_NetworkAdapterConfiguration|?{$_.ipenabled}).IPAddress
  • .Net
  • [net.dns]::GetHostAddresses("") | Select -ExpandProperty IPAddressToString

    …shortened to:

    [net.dns]::GetHostAddresses("")|Select -Expa IP*

    or

    [Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().UnicastAddresses|%{$_.Address}
  • the Test-Connection cmdlet
  • Test-Connection -ComputerName ::1 -Count 1 |             
    Select-Object -Property IPv*Address            
    

    …shortened to

    Test-Connection ::1 -Cou 1|select I*
  • the registry
  • Get-ChildItem -Path HKLM:\system\CurrentControlSet\services\Tcpip\Parameters\Interfaces|             
    ForEach-Object {            
            Get-ItemProperty $_.PSPath -Name *IPAddress -ErrorAction SilentlyContinue            
    } | Select-Object -Property *IPAddress

    …shortened to:

    $key = "HKLM:\system\CurrentControlSet\services\Tcpip\Parameters\Interfaces"            
    gci $key|%{gp $_.PSPath -N *IPAdd* -EA 0}|select *IP*
  • regular expressions and local executables like
    • ipconfig.exe
    • foreach($ip in (ipconfig) -like '*IPv4*') {            
          ($ip -split ' : ')[-1]            
      }

      …shortened to

      (ipconfig)-like'*IPv4*'|%{($_-split': ')[-1]}
    • netsh.exe
    • (netsh interface ipv4 show addresses) -match             
      'Address' -replace '.+:\s+(\S+)', '$1'

      …shortened to

      (netsh i ip sh ad)-match'Address'-replace'.+:\s+(\S+)','$1'
    • systeminfo.exe
    • systeminfo|%{@(([regex]"\s{33}\[\d{2}\]:\s(?<I>.*)").Matches($_).Groups)[-1].Value}
      

      NB: This one is more unsual than traditional, I’ve used it in the Scripting Games 2012 as far as I remember and proposed it for fun in the brain teaser šŸ˜‰

Advertisements

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