WMI Server Side Filtering

by Apr 15, 2010

Whenever you use Get-WMIObject, be sure to minimize resources and maximize speed by using server-side filtering. The next line will use the slow client-side filtering with Where-Object:

Get-WMIObject Win32_Service |
? {$_.started -eq $false} |
? { $_.StartMode -eq 'Auto'} |
? { $_.ExitCode -ne 0} |
Select-Object Name, DisplayName, StartMode, ExitCode

A faster and better approach is to filter on the WMI end by using the query parameter:

Get-WMIObject -Query ('select Name, DisplayName, StartMode, ' +
'ExitCode from win32_Service where Started=false ' +
'and StartMode="Auto" and ExitCode<>0')

Twitter This Tip! ReTweet this Tip!