Prof. Powershell

Pipeline Power

Continuing our refresher on PowerShell basics, we'll revisit the pipeline.

Continuing our refresher on PowerShell basics, we'll revisit the pipeline.

Last week we looked at objects in PowerShell. Most often cmdlets are designed to emit objects. These objects are "written" to a construct we refer to as a pipeline. Think of it as a literal pipe.

PS C:\> Get-Service

Get-Service retrieves all service objects on my computer and sends them through the pipeline.

Figure 1. Get-Service retrieves all service objects (those blue boxes) on my computer and sends them through the pipeline.

While the objects are in the pipeline I can do anything I want. But if I do nothing, they come out at the end of the pipeline and PowerShell examines them with a cmdlet called Out-Default. In my example, PowerShell determines these are service objects, checks its rules and sends formatted text to another special cmdlet called Out-Host which results in the text output you see.

Out-Default and Out-Host at work.

Figure 2. Do nothing else and Out-Default checks out those services and spits out info via the Out-Host cmdlet. (Click image to view larger version.)

It is important that you remember that unless something is written to the pipeline you won't get anything out of the other end. Some cmdlets by design do NOT write objects to the pipeline.

PS C:\> Get-Service wsearch | stop-service

Stop-Service doesn't write to the pipeline.

Figure 3. Stop-Service doesn't write to the pipeline.

However, we can force the cmdlet to write objects to the pipeline using the -Passthru parameter. You'll see this on any cmdlet which by default does not write to the pipeline.

PS C:\> Get-Service wsearch | Stop-Service -passthru

Status   Name     DisplayName
------   ----     -----------
Stopped  wsearch  Windows Search

To get results from Stop-Service, add the -Passthru parameter.

Figure 4. To get results from Stop-Service, add the -Passthru parameter. (Click image to view larger version.)

Now I get pipelined output. I can't stress the importance of understanding where pipelined output comes from otherwise you'll end up with nothing when you are expecting something.

When you save a PowerShell expression to a variable, it only works if there is pipelined output.

PS C:\> $s=Get-Service wsearch | Stop-Service
PS C:\> $s
PS C>\>

My variable, $s is empty because Stop-Service didn't write to the pipeline.

PS C:\> $s=Get-Service wsearch | Stop-Service -passthru
PS C:\> $s

Status   Name     DisplayName
------   ----     -----------
Stopped  wsearch  Windows Search

but now it does.

As long as there are objects in the pipeline I can manipulate them anyway I want, simply by piping one command to another. And none of this requires any programming or scripting.

PS C:\> get-service | where-object {$_.status -eq "running"} | Sort-object -property Displayname | Select-Object -property Displayname

The first command returns all service objects which are then passed to the Where-Object cmdlet which looks at the status property of every incoming object (referenced by $_). If the status is equal to running the object is passed on to the pipeline and handed off to Sort-Object. This cmdlet sorts the running service objects on the Displayname property and then pipes this collection to Select-Object which only returns the Displayname property. (Technically, Select-Object is creating a custom object with a single property.)

At the end of the pipelined expression Out-Default handles the rest and you get a text list of all your running services by display name.

Next time, we'll look at another pipeline technique: pipeline binding.

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