Thursday, 29 January 2009

Get-IPAddress.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     Gets and displays the IP address of a computer 
  4. .DESCRIPTION 
  5.     This script uses Win32_NetworkAdapterConfiguration to 
  6.     obtain, then display, a system's IP addresses. 
  7.     NB: this only works on XP or later versions of Windows. 
  8. .NOTES 
  9.     File Name : Get-IPAddress.ps1 
  10.     Author : Thomas Lee - tfl@psp.co.uk 
  11.     Requires : PowerShell V2 
  12. .LINK 
  13.     Script postesd to: 
  14.     http://www.pshscripts.blogspot.com 
  15.     MSDN Sample at: 
  16.     http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx 
  17. .EXAMPLE 
  18.     [ps] c:\foo> .\Get-IPAddress.ps1 
  19.     IP Address   : 10.10.1.115 
  20.     IP Address   : fe80::3953:f67b:2f1c:1323 
  21.     IP Address   : 10.10.1.120 
  22.     IP Address   : fe80::d8ed:afe2:2a97:a596
  23.     4 IP addresses found on this system
  24. #> 
  25.    
  26. ## 
  27. # Start of Script 
  28. ## 
  29.    
  30. # Get Networking Adapter Configuration 
  31. $Computer = "." 
  32. $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration 
  33.    
  34. # Iterate and get IP address 
  35. $count = 0 
  36. foreach ($IPConfig in $IPConfigSet) { 
  37.    if ($Ipconfig.IPaddress) { 
  38.       foreach ($addr in $Ipconfig.Ipaddress) { 
  39.       "IP Address   : {0}" -f  $addr
  40.       $count++  
  41.       } 
  42.    } 
  43. if ($count -eq 0) {"No IP addresses found"
  44. else {"$Count IP addresses found on this system"
  45. #End of Script 

6 comments:

sri said...

The program is nice.
But i know my ip address through this ip-details along with router map.

Thomas Lee said...

SRI: what the ip-details site shows you is the external address, not yor internal address. The script shows how to get your internal address (or addresses).

Ryan M. Ferris said...

If I parse out 'ipconfig' in Powershell as below I get five addresses which System.Net.IPAddress validates and can piped to ping.exe as strings. If I use 'gwmi' (as below) I am returned three addresses whose IPv6 addresses don't validate in System.Net.Ipaddress or work with ping.exe but are the identical format used in Win 7 firewall logs. What pieces of Windows Networking knowledge am I missing?

$IPAddress_ipconfig=(ipconfig | Select-string Address) -split ": " | Select-string -notmatch ". ."
foreach ($IP in $IPAddress_ipconfig) {([System.Net.IPaddress]::Parse("$IP")).IPAddressToString}
fe80::6172:6ecf:2d05:b0ae%12
192.168.0.11
fe80::cddc:ceef:b717:a5ac%11
2001:0:4137:9e76:30c3:129:3f57:fff4
fe80::30c3:129:3f57:fff4%17

Get-WmiObject Win32_NetworkAdapterConfiguration | ? {$_.IPAddress} | Select IPaddress | fc -expand CoreOnly | findstr [0-9]

fe80::cddc:ceef:b717:a5ac
192.168.0.11
fe80::6172:6ecf:2d05:b0ae

Thomas Lee said...

The last two addresses shown via IPConfig are for the unnel adapter Teredo Tunneling Pseudo-Interface. It appears that WMI does not return these.

Dean Journey said...

Where would I add a line to also get the computer name

Thomas Lee said...

In line 31, I set the computer name to "." (aka local host). If you wanted to get the hostname just used the hostname.exe eg:

$cname = hostname
"This computer is named {0}" -f $cname

Hope this helps!