Search code examples
powershellwmi

How to use LIKE operator in WMI queries?


I try to get info about DHCP Leases via WMI, so I use this code: gwmi -ComputerName dhcp -query "select DHCPLeaseExpires, ipaddress from Win32_NetworkAdapterConfiguration", but I'd like to make condition more precise. I'd like to filter all rows with certain IPAddress in it.

For example, I have this output:

DHCPLeaseExpires :
IPAddress        :
PSComputerName   :

DHCPLeaseExpires : 20200603123623.000000+120
IPAddress        : {172.21.0.100}
PSComputerName   :

DHCPLeaseExpires : 20200603123623.000000+120
IPAddress        : {10.10.10.10, fe80::cc09:dfe8:d8b8:99f8}
PSComputerName   :

How can I use LIKE operator to filer out all entries but 10.10.10.10?

Tried this: gwmi -ComputerName dhcp -query "select DHCPLeaseExpires, ipaddress from Win32_NetworkAdapterConfiguration where IPAddress like '%10%'" but got InvalidArgument error. Also tried "%10%", \"%10%\", etc.


Solution

  • IPAddress is an Array.

    So you could use Where-Object with -contains or -notcontains

    For instance like this:

    gwmi -ComputerName dhcp -query "select DHCPLeaseExpires, ipaddress from Win32_NetworkAdapterConfiguration" | 
        Where-Object IPAddress -contains 10.10.10.10