WMI LIKEs Wildcards

You may hear or read that WMI can’t accept wildcards. WRONG

WMI accepts wildcards but not the ones you might expect.

 

Consider

Get-Process p*

This gets all the process that begin with the letter p

To do something similar with WMI we need to use the –Filter parameter. We can get a single process like this

Get-WmiObject -Class Win32_Process -Filter "Name='powershell.exe'"

so you may want to try this

Get-WmiObject -Class Win32_Process -Filter "Name='p*'"

 

Oops no returns of any kind.

That’s because in the filter we are using WQL which uses the SQL wildcards

% = *   multiple characters

_ = ?    single character

 

OK then this will work

Get-WmiObject -Class Win32_Process -Filter "Name='p%'"

 

Oh no it won’t because a further complication is that we have to use the WQL LIKE operator not =

Get-WmiObject -Class Win32_Process -Filter "Name LIKE 'p%'"

And we have a winner

 

To use the single character wildcard (which in my experience doesn’t get used as much as the multi-character)

Get-WmiObject -Class Win32_Process -Filter "Name LIKE 'powershell.e_e'"

 

And as an added bonus the title of the post give us a way to remember to use the LIKE operator.

Published Mon, Dec 12 2011 21:50 by RichardSiddaway
Filed under:

Comments

# re: WMI LIKEs Wildcards

Thanks!

Excellent explanation and it helped me a lot :)

Wednesday, January 02, 2013 4:27 AM by Xcom3

# re: WMI LIKEs Wildcards

Thank you so much :-)

You made my day..

IT WORKS :D

Monday, March 25, 2013 10:56 AM by Mr_Latein

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: