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

Filter Left

Posted on October 14, 2011October 14, 2011

When writing WMI queries expressions in Windows PowerShell, it is recommended to use WMI filtering, as opposed to getting objects and then filtering with Where-Object. I see expressions like this quite often:
[cc lang="PowerShell"]
get-wmiobject win32_process -computer $c | where {$_.name -eq "notepad.exe"}
[/cc]
In this situation, ALL process objects are retrieved and THEN filtered. The better performing approach is to use a WMI filter:
[cc lang="PowerShell"]
get-wmiobject win32_process -filter "name='notepad.exe'" -computer $c
[/cc]
The WMI service on the remote computer filters in place and you only get back the item you want. Don't believe me? Measure for yourself. Start up Notepad, then define these script blocks.
[cc lang="PowerShell"]
PS C:\> $a={gwmi win32_process | where {$_.name -eq "notepad.,exe"}
PS C:\> $b={gwmi win32_process -filter "name='notepad.,exe'"}
[/cc]
Now measure how long it takes the first to run:
[cc lang="PowerShell"]
PS C:\> Measure-command $a
[/cc]
WMI caches results so wait about 10 minutes and then measure the second script block.
[cc lang="PowerShell"]
PS C:\> Measure-command $b
[/cc]
For me, the second expression took half as long. Granted this is a small data set and I'm not going to quibble over 100ms. But when you think about querying many computers with the potential for larger data sets, the performance gains are significant. So get in the habit of filtering as far to the left as you can in your PowerShell expressions.

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!

[this was originally posted in my Google+ account.]


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