Where-object vs WQL

No matter what you’re doing in PowerShell there are guaranteed to be at least 5 different ways to accomplish your goal.  Early on I would suggest focusing on the functionality, but after your script is working as intended.  A little time spent tuning and optimizing your various functions and loops will go a long way.   Sometimes an IF statement is faster than a switch. Likewise while vs. until vs. foreach… Take the time and try the various options.  Performance varies greatly depending on what type of object you’re working with, and what you’re doing with that object.   So far in my quest for posh speed I have found two things to be globally true:

  1. The less data you pass down the pipe line the faster your script will run.
  2. 99% of the time filtering at the source is 25%-50% faster then using select-object/where-object.

Along those lines I FINALY figured out how to do a “where not” WQL query!  Low and behold Glenn’s global laws of performance held true again…

If execution times are keeping you in the dark ages of automation.  Look no further than your own code.  I am yet to find a task where PowerShell doesn’t crush VBscript/WSH/BAT.


Leave a Reply