#PSTip Optimizing WMI object retrieval

Whenever we use WMI cmdlets to query either local or remote machines, we need to keep the following points in mind:

Don’t ever pipe the WMI objects to Where-Object cmdlet to filter the WMI output unless there is a need to do so. Consider this example:

Get-WmiObject -Class Win32_OperatingSystem -ComputerName $servername | Where-Object { $_.BuildNumber -eq "7601" }

The above command forces WMI to retrieve the objects to the local session and pass them on to Where-Object for filtering. This takes longer depending on the number of objects to retrieve or the size of the objects. This is where the –Filter parameter of the Get-WmiObject comes into play.

Get-WmiObject -Class Win32_OperatingSystem -ComputerName $servername -Filter "BuildNumber=7601"

In this example using –Filter parameter, the filtering happens on the remote system and results in faster execution of the command.

WMI speed up

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: