Windows PowerShell – Pipeline Symbol (|) or (¦)

PowerShell Scripting – Pipeline Symbol (|) or (¦)

Pipelining could almost be described as PowerShell’s signature tune.  Windows PowerShell encourages you to join two statements so that the output of the first clause, becomes the input of the second clause. 

Windows PowerShell Pipeline (|) Topics

 ♣

Pipeline (|) or (¦) – Possible Display Confusion

When typed in notepad the pipeline symbol looks like a solid vertical bar |, but when typed at the Windows PowerShell PS> prompt, it looks like ¦.  On my keyboard the key in question is next to the Z; however I have seen keyboards where the key is next to numeric 1 on the top row.  Once you press the correct key, you get a pipe or bar symbol like this: |. 

Michael Babbitt writes to say that on his American keyboard the pipe is above the \ (backslash) and next to the right hand ‘Shift’ key.

To be clear, this pipe (|) symbol corresponds to ASCII 124, and not ASCI 0166.  Test by holding down the Alt key, then type the number 124 on the numeric pad, finally, let go of the Alt key you should get a |.

Pipeline Examples

Here are examples showing how to join two or more clauses to form a continuous PowerShell production line.  Check the logic.  See how the output from the first clause becomes the input for the second statement.

Example 1: Task Manager Processes

The problem: Get-Process produces too much output.
The solution call for a ‘where’ filter.

# Simple PowerShell Pipeline Example
Get-Process | Where-Object {$_.handlecount -gt 100 }

Example 2:  Too Much!

The problem: Get-Process produces too much output.
The solution call ‘More’ so that the output pauses between screen fulls.

# Simple PowerShell Pipe Example
Clear-Host
Get-Process | more 

Example 3: Two PowerShell Pipes

The problem: We need to control the properties
The solution: A second pipe, then control the display with Format-Table

Get-Process `
| Where-Object {$_.company -Notlike ‘*Microsoft*’}`
| Format-Table ProcessName, Company -auto

Note: ` (Backtick) means continue on the next line.  I decided to use three lines to make the pipes standout.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

$_ Placeholder Meaning In the Current Pipeline

$_  is special PowerShell placeholder, which references a property in the current pipeline.  In the above example, that current property is .handlecount, hence, $_.handlecount.  I feel certain that $_ is a PowerShell feature that you will employ in numerous scripts, thus it is worth memorizing the rhythm of the command:
dollar / underscore / dot property.  For example $_.name.  To put this in context

# PowerShell $_ Placeholder Example
Get-Help * | Where { $_.name -Match "get" } | FT name, synopsis -auto

Another Example of the PowerShell $_

# PowerShell $_ Placeholder Example
Get-Help * | Where { $_.synopsis -Match "object" } | ft name, synopsis ¦ auto

The idea is to list all PowerShell items which have the word "Object" in their synopsis.  Trace how the example is constructed from three clauses separated by two | pipes.

The Importance of PowerShell’s Pipeline

If it helps to streamline your task, you can have more than one join per statement, thus the output of the second clause, becomes the input of the third element.  To separate these individual clauses, Microsoft chose the pipe symbol, ‘|’ sometimes called the bar key.

To give you a physical analogy, think of an oil pipeline with lots of cylinders joined together with hollow ring seals.  Or better still, an assembly line to produce a bottle of beer!

One common use of pipelining is the ‘Where’ clause, for example:
Get-Eventlog system | where{ $_.eventId -eq 17}.  Incidentally, that commands finds Windows Update Agent activity.

I also use the pipe for research, my technique is to place ‘|’ between the object I am investigating and Get-Member.  For example, Get-Service | Get-Member.

Summary of the Pipeline Symbol |Windows PowerShell’s Signature

What makes PowerShell modular is the ability to pipe the output of the first command so that it becomes the input of the second command.  When you need to refine a script | filter the output | or format the display, then call for the pipe symbol (bar key) to join each statement, and thus construct your pipeline of PowerShell commands.

The confusion of the pipeline symbol (|) is because the character corresponding to ASCII 124 displays differently in Notepad compared with when typed at the PowerShell command line.  Once you have the correct symbol you will find numerous uses to combine, or pipeline, two or three clauses to make a punchy PowerShell command.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.