Chapter 10. Avoiding the pipeline

 

Jeff Wouters

Whether you’re new to PowerShell or you’re at a more advanced level, I encourage you to always consider performance and execution time when you write a script.

One of the most powerful features of PowerShell is its ability to use the pipeline. Finding objects, filtering them to return the ones you want, and performing an action on those objects is easy to do with pipelined expressions, which I refer to as the pipeline. Every step is one pipe in the pipeline. In general, the fewer pipes you use, the shorter the execution time and the fewer resources that are used.

Although creating one-liners is easy to learn and understand, following best practices yields the best performance. If you don’t implement best practices your script may still work, but you’ll experience negative performance and/or long execution times.

When writing scripts I always keep this goal in mind: to complete the task at hand in the most efficient way.

In this chapter I’ll show you how to combine parameters so that you won’t have long commands in which objects are piped from one cmdlet to another. This approach improves performance, decreases the execution times of your scripts, and generally reduces the amount of code you write.

Requirements

The only requirement for this chapter is the ability to execute PowerShell code. You have a few ways to accomplish this:

Rules of engagement

Filtering objects sooner

Providers and filtering parameters

Using regular expressions

Using member enumeration

Summary

About the author