PowerShell for Windows Admins

Dec 12 2011   3:51PM GMT

WMI LIKEs Wildcards

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: