Windows PowerShell


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.  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.

Windows PowerShell Pipeline Topics

Pipeline (|) or (¦) - 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: |.

To be crystal clear, this pipe symbol, | corresponds to ASCII 124.  N.B this 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.

Pipeline Examples

Here are four 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:

get-Process.  Let us discover the members and properties
get-Process | get-Member

Example 2:
get-Process | more  (Meaning pause between screen-fulls)

Example 3:
get-Process.  Produces too much output, let us filter with a where statement.
get-Process | where {$_.handlecount -gt 100 }

Example 4:
get-Process | where {$_.handlecount -gt 100 } | format-Table ProcessName, handles.

The last example has two pipe symbols.  You may observe that either 'where-object', or plain 'where', work equally well. Also, -gt means greater than.

$_ Placeholder 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

get-help * | where { $_.name -match "get" } | ft name, synopsis -autosize

Another example of $_
get-help * | where { $_.synopsis -match "object" } | ft name, synopsis -autosize

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.

  ˚

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.

See Also

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.