
close
close
If you want to really understand PowerShell, then it’s essential that you understand the PowerShell pipeline. It’s critical to understand this key concept because everything you do in PowerShell takes place in the pipeline, and as such, you’ll find different ways to take advantage of it.
To begin, let’s think of the pipeline as the length of a pipe. Commands go in one end, and objects come out the other. You can’t always see what’s happening inside the pipe, but you can provide some direction. In PowerShell, the vertical bar ( | ) is the pipe symbol. This tells PowerShell that you want to take the output of one command and pass it as the input (or pipe it) to the next command. This concept has been around a long time in console-based shells and is hardly new to PowerShell. But a major difference is that instead of piping or passing text between commands, you are passing complete objects.
Let’s look at an easy example. When you run Get-Service, the cmdlet creates the necessary objects to represent the services on your computer, and PowerShell displays the results.
A simple PowerShell command (Image Credit: Jeff Hicks)
advertisment
Pipeline input by value (Image Credit: Jeff Hicks)
A pipelined expression (Image Credit: Jeff Hicks)
Using a pipelined expression is a great technique for refining results until you get exactly what you need. For beginners, I recommend breaking this down into several steps. Here’s an example.
First, I need to sort all processes by their Virtual Memory in descending order.
advertisment
get-process | sort -Property "VirtualMemorySize" -Descending
I already know that my simple Get-Process command works otherwise I would have run that first by itself. If this gives me the results I’m expecting then I can add the next pipelined step:
get-process | sort -Property "VirtualMemorySize" -Descending | Select -first 10
A longer pipelined example (Image Credit: Jeff Hicks)
get-process | sort -property "VirtualMemorySize" -descending | Select -first 10 | measure-object VirtualMemorySize -sum –average
The complete PowerShell expression (Image Credit: Jeff Hicks)
advertisment
This is only a two-step pipelined expression, but I think you’ll get the idea. I could run this:
dir c:\scripts\*.ps1 -Recurse | measure-object -Property length –sum
That works just fine, and took a little over two seconds to execute. I could also break it into steps:
$files = dir c:\scripts\*.ps1 –Recurse $files | measure-object -Property length -sum
The first command took 974 milliseconds, and the last command took 67 milliseconds, which taken together is still slightly faster. I’ll admit this isn’t a thrilling example, but I hope it makes my point.
Using pipelined expressions in PowerShell lets us do things that used to require complicated batch or VBScripts. Not anymore, as long as you think about passing objects in the pipeline.
You can learn much more about PowerShell pipelines by reading the help topic About_Pipelines.
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri