The Surly Admin

Father, husband, IT Pro, cancer survivor

Powershell and the Pipeline

Continuing the “Getting Started” series with a talk about the 7 major things you’ll use in Powershell.  This is by no means a comprehensive list of how to use Powershell but something to get you started.  One of the more powerful features of Powershell is the pipeline.  If you ever used DOS and batch programming you know all about the pipeline and Microsoft has really embraced it with Powershell.

The idea behind the pipeline is that you take the results of one thing and “give” it to another.  You can pipe the results of one cmdlet into another cmdlet.  You can pipe a variable into the pipe and have the next cmdlet down the line work with it.  You can pipe an array into the ForEach-Object cmdlet and walk your way through every element in the array.

In more advanced Powershell scripting you will begin outputting your results as objects so they can be manipulated using Where, Select, Sort and more.  The pipeline is one of the key components to truly unlocking the power of Powershell.

Remember that Get-Service cmdlet we’ve been using?  Let’s use the pipeline to clean up the output so it shows us what we want to see, in a pleasing manner.  Running Get-Service by itself outputs some pretty decent information, but let’s say you want to know the service name too.  Powershell will only show the “default” variables and on Get-Service that does not include service name, so we have to tell it specifically what we want.  We’ll use the Select cmdlet to do this:

Get-Service | Select Name,DisplayName,Status,ServiceName

Which is good, but notice how Powershell is truncating some of the fields?  This is default behavior for Powershell but we can fix that too.  Another set of formatting cmdlet’s is Format-List, Format-Table, Out-Gridview (my favorite) and quite a few more.  Let’s try this

Get-Service | Select Name,DisplayName,Status,ServiceName | Format-Table -AutoSize

A little better, but still truncating the last field (Powershell does this depending on the size of the window so your results may be a little different).  There’s another parameter on Format-Table we can use called -Wrap.

Better, at least it’s showing us everything now.  But my favorite is Out-Gridview which creates an interactive window for you to look at the data.  Try this:

Get-Service | Select Name,DisplayName,Status,ServiceName | Out-Gridview

No screen shot here, run it yourself.  The Out-Gridview is really cool and invaluable when working on a new script–though I rarely use it for final output.

What if you only want to display services from Adobe?  You can use the Where cmdlet and the pipeline for that.

Get-Service | Where { $_.ServiceName -like "*Adobe*" } | Select Name,DisplayName,Status,ServiceName | Out-Gridview

Typically you want your Where cmdlet to be as close to the original cmdlet as possible, this is so anything “down the line” doesn’t have to deal with as much data.  In this Where we are checking the pipeline variable $_, and the specific property of that object .ServiceName and seeing if the word “adobe” appear in it.  If it does, pass it down to Select to get the fields I want and then pass it down to Out-Gridview to give me a nice looking display.

For fun, let’s sort this by Status.

Get-Service | Where { $_.ServiceName -like "*Adobe*" } | Select Name,DisplayName,Status,ServiceName | Sort Status -Descending | Out-Gridview

I hope this has given you a basic taste of the pipeline and what it’s capable of doing.


The big take-away here are there are a few building block cmdlet’s that you can use to get you started on Powershell.  Remember that a lot of the information you need is right in Powershell itself, and frankly there’s very little you are trying to do that hasn’t been attempted before so Google will be a very valuable resource.  Here’s the big guns, in my view:

  • Get-Command
  • Get-Help
  • .Gettype()
  • Get-Member
  • ForEach
  • IF
  • Pipeline

Master those and you are well on your way.


September 27, 2012 - Posted by | Powershell - Getting Started |

1 Comment »

  1. […] using Powershell? Powershell Discovery: Variables Get Started with Powershell: Loops and branches Powershell and the Pipeline Powershell Best Practices – Surly […]

    Pingback by Powershell – Steep Learning Curve? « The Surly Admin | March 28, 2013 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: