Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: Format Get-WmiObject output to return only the IP address

Tags:

powershell

wmi

I would like to use Get-WmiObject Win32_NetworkAdapterConfiguration to return the ip address of a network card. Unfortunately, I cannot figure out how to format the output to display only the IPv.4 address.

Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}

Displays:

IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}

Using the above output as an example, I would like it to only return 192.168.56.1 (Some clients have multiple NIC's, hence the "Where-Object")

like image 582
pizzim13 Avatar asked Jul 12 '10 18:07

pizzim13


2 Answers

The IPAddress property is a string[], so the following should do it:

gwmi Win32_NetworkAdapterConfiguration |
    Where { $_.IPAddress } | # filter the objects where an address actually exists
    Select -Expand IPAddress | # retrieve only the property *value*
    Where { $_ -like '192.168.*' }
like image 93
Joey Avatar answered Nov 02 '22 18:11

Joey


Adding a quicker answer (avoiding Where-Object and using -like operation on a list):

@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]

Hope this Helps

like image 21
Start-Automating Avatar answered Nov 02 '22 18:11

Start-Automating