The Pipeline

The pipeline is a feature of PowerShell that takes the output of one cmdlet and sends ("pipes") it to another cmdlet—as you may have noticed in some of the examples earlier in this chapter. The PowerShell pipeline is very similar to the Pipeline in Unix and Linux—and is perhaps one of PowerShell's more significant features. One major difference is that in Unix/Linux, what is usually sent between stages in the pipeline is raw text that then needs to be manipulated—often referred to as prayer-based parsing. For example, you could use the Unix command PS to get a list of processes; then you could send the text output to a formatting program to manipulate the output. PowerShell sends .NET (or other) objects between stages in the pipeline.

For example, you could pipe the output of the Get-Process cmdlet into the Sort-Object cmdlet as follows:

PSH [D:\foo]: get-process | sort-object handles

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
      0       0        0         28     0               0 Idle
     21       1      192        432     4     0.22   1568 smss
     31       3     1224       3072    32     0.05    968 shstat
     35       2     1040       3212    27     0.06   3940 Mctray
... {rest of table removed}

In this example, the Get-Process cmdlet obtains a set of process objects representing the processes running on the system, and sends them to the next stage of the pipeline. These process objects are then input to the Sort-Object cmdlet that sorts the process objects based on the number of active handles. This is then output to the console ...

Get Windows Server 2008: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.