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.
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 |.
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.
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)
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.
$_ 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.
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
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.