Common mistakes–filtering

Which of these should you use

Get-WmiObject -Class Win32_LogicalDisk | where DriveType -eq 3


Get-WmiObject -Class Win32_LogicalDisk -Filter {DriveType = 3}

They both give the same results after all.

Against a remote machine the first one took 640 milliseconds and the second took 245 milliseconds.

The reason for the difference is that the first command pulls back information on all logical disks – and then filters.  The second on pulls back information on drive type 3 (local hard disks)

The 395 milliseconds difference doesn’t sound much and against 1 machine is negligible.  No span that across hundreds or thousands of machines and you get a significant impact.

Always filter the data down as soon as you can – preferably at source.

This entry was posted in PowerShell and WMI, Powershell Basics. Bookmark the permalink.

One Response to Common mistakes–filtering

  1. Marius says:

    Great article. (maibe an example with eventslog records would be more practicle and more ovious = bigger difference ? )


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s