Prof. Powershell

The Big Kahuna

Ride the pipeline -- you'll love its automation power.

Understand how to use the pipeline in PowerShell and you'll feel like you upgraded from a family sedan to a Ferrari. You will make your automation that much more powerful and it will be a lot of fun to drive.

When you execute a PowerShell cmdlet the result is a group of objects, not text like you get in a CMD session. If you don’t ask PowerShell to do anything else with these objects, they get passed down the pipeline and out the other end. At the end, PowerShell turns to its internal rules and returns a textual representation of the remaining objects in the pipeline.

So, when you run:

Get-Service

you'll see a text list of all service objects on your computer. But because these are objects, you can pass them to another cmdlet, such as a filtering cmdlet, to only get services that are stopped:

Get-Service | Where {$_.status -eq "Stopped"}

With this, you still get a text list, but now you'll only see the service objects where the Status property is set to stop.

The $_ in the Where cmdlet is a special variable that represents the current object in the pipeline. Taht is, after Get-Service finishes, all of the service objects are passed to the next cmdlet. This cmdlet enumerates them one by one, using $_ to represent the current object. It checks the Status property and if it is "Stopped," keeps the object. All others are tossed from the pipeline.

But you may be wondering how I knew the property name. Easy -- I just asked PowerShell:

Get-Service | Get-Member

The Get-Member cmdlet will display all properties and methods of all objects passed to it. Looking at the list, I took a lucky guess that Status was the likely candidate. I also had a clue because the default output used Status as a column heading. But don't always trust that a column heading is the same as the property name.

Let's quickly return to the Get-Service example. You can continue piping objects to as many cmdlets as you like, each time manipulating the objects in the pipeline. The following is a single line command:

Get-Service | Where {$_.status -eq "Stopped"} | Sort DisplayName | Select DisplayName

I'll cover other cmdlets in more detail in future columns, but if you run this one, you will get a sorted list of the displayname for all stopped services. You won't have to parse any text, use any scripts or functions -- only the pipeline and PowerShell cmdlets. And, you can do it in one line.

You can use just about any expression, cmdlet, scriptblock or function in the pipeline, as long as the output is an object. You'll see a lot more of the pipeline. Surf's up!

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events