Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get IP Addresses with PowerShell

Posted on June 6, 2012

In celebration of World IPv6 Day, I thought I'd post a little PowerShell code to return IP addresses for a computer. This information is stored in WMI with the Win32_NetworkAdapterConfiguration class. This class will return information about a number of virtual adapters as well so I find it easier to filter on the IPEnabled property.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The returned object has an IPAddress property which is an array of addresses. In here you will find IPv4 and IPv6 addresses. My quick and dirty script uses a relatively simple regular expression pattern to get those addresses.


Param([string]$computername=$env:computername)

[regex]$ip4="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"

get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'" -computer $computername |
Select DNSHostname,Index,Description,@{Name="IPv4";Expression={ $_.IPAddress -match $ip4}},
@{Name="IPv6";Expression={ $_.IPAddress -notmatch $ip4}},MACAddress

In the Select-Object expression I'm defining custom properties to get the different IP addresses. Here's what it looks like in action.


PS E:\> C:\scripts\Get-IP.ps1 -comp quark

DNSHostname : Quark
Index : 10
Description : Realtek PCIe FE Family Controller
IPv4 : 172.16.10.126
IPv6 : fe80::ddcf:c714:44c8:bcc3
MACAddress : C8:0A:A9:04:C5:32

If you had a number of computers you could pipe the results to Where-Object and get just the IPv6 machines (or not).


... | where {$_.Ipv6} | Sort DNSHostname | Select DNSHostname,Description,IPv6

Feel free to download Get-IP and try it out for yourself.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d