O'Reilly logo

Windows PowerShell Cookbook, 3rd Edition by Lee Holmes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2. Pipelines

Introduction

One of the fundamental concepts in a shell is called the pipeline. It also forms the basis of one of PowerShell’s most significant advances. A pipeline is a big name for a simple concept—a series of commands where the output of one becomes the input of the next. A pipeline in a shell is much like an assembly line in a factory: it successively refines something as it passes between the stages, as shown in Example 2-1.

Example 2-1. A PowerShell pipeline

Get-Process | Where-Object WorkingSet -gt 500kb | Sort-Object -Descending Name

In PowerShell, you separate each stage in the pipeline with the pipe (|) character.

In Example 2-1, the Get-Process cmdlet generates objects that represent actual processes on the system. These process objects contain information about the process’s name, memory usage, process ID, and more. As the Get-Process cmdlet generates output, it passes it along. Simultaneously, the Where-Object cmdlet gets to work directly with those processes, testing easily for those that use more than 500 KB of memory. It passes those along immediately as it processes them, allowing the Sort-Object cmdlet to also work directly with those processes and sort them by name in descending order.

This brief example illustrates a significant advancement in the power of pipelines: PowerShell passes full-fidelity objects along the pipeline, not their text representations.

In contrast, all other shells pass data as plain text between the stages. Extracting meaningful ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required