PowerShell primers

PowerShell: Learn to tap the pipeline

How to use the humble pipe character to string together PowerShell cmdlets to format, filter, sort and refine results.

Become An Insider

Sign up now and get FREE access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content. Learn more.

Microsoft PowerShell is a powerful scripting language and administrative framework for Windows, and one of the key elements that makes it so powerful is the pipeline — the assembly line of data and results that moves between and through cmdlets. In this piece, we’re going to talk about how you glue stuff together — or, more specifically, how you take the output or results from one PowerShell cmdlet and send it into another for further processing.

This is called piping, and the invisible tube that connects one cmdlet to another is the pipeline. The character that represents this all:

|

It’s known as the pipe and is the character above the backslash on your keyboard.

Tapping the pipeline

I think the best way to demonstrate the pipe and a pipeline is to do a simple example. But before we do that, I need to introduce two helpful features of PowerShell:

  • format-list, which takes the output of almost any cmdlet and formats it in a list that explodes all relevant details
  • format-table, which formats output in a nice text-based table

Format-list and format-table are absolutely dependent on the pipeline. You can’t just issue a format-list command — there has to be data to format in the first place. You get that data to the format-list cmdlet through the pipeline.

Remember our get-process cmdlet from my first article on PowerShell basics? Let’s practice pipelining by asking it to give us more information on the Google Chrome browser process formatted as a list:

get-process chrome | format-list

Here’s what we get back:

PowerShell getprocess formatlistJonathan Hassell / IDG

There are all of the Chrome processes on my machine right now, formatted as a list, with their properties exposed and expanded. We took the output of get-process chrome and piped it using the | character into the format-list cmdlet.

Filtering and limiting

One of the most common uses of pipelining is to take the output of one cmdlet and then filter it down into a certain subset of results; once you have filtered out the “noise” and you have your desired results, you then pipe that subresult set into another cmdlet to do some further magic.

This is where the where-object cmdlet comes in. Where-object is one of the filtering mechanisms in PowerShell, and you use it by putting together “where clauses.” Now, the formatting of where-object gets a little funky, so stay with me while I show it to you.

To continue reading this article register now

Shop Tech Products at Amazon