How to Send a Command's Output to Another Command with a Pipe in PowerShell
The pipe, |, sends what one PowerShell command outputs into the next command, so small commands combine into one. This guide sorts a list of fruit with Sort-Object, keeps the first two with Select-Object, and shows that the pipe passes objects one at a time, with real output from PowerShell 7.6.10.
The about_Pipelines page: "A pipeline is a series of commands connected by pipeline operators (|) (ASCII 124). Each pipeline operator sends the results of the preceding command to the next command."
Syntax
<command1> | <command2> | <command3>
| Part | What it means |
|---|---|
<command1> | The first command. Its output goes into the pipe |
| | The pipeline operator: sends the results of the command on its left to the command on its right |
<command2> | Receives the output of <command1>, works on it, and sends its own output on |
<command3> | More commands can follow, each after its own pipe. The last command's output is printed |
The about_Pipelines page: "The output of the first command can be sent for processing as input to the second command. And that output can be sent to yet another command." And: "In a pipeline, the commands are processed in order from left to right."
The Example
The example works on a list of three fruit names, 'fig', 'banana', 'apple': commas between the values make a list. The commands run in the PowerShell 7.6.10 console.
Prerequisites
- PowerShell 7. This guide uses PowerShell 7.6.10:
$PSVersionTable.PSVersion.ToString()printed7.6.10.
Step 1: Send Output to Another Command
'fig', 'banana', 'apple' | Sort-Object
apple
banana
fig
The list on the left of the pipe went into Sort-Object, which printed it in alphabetical order. The Sort-Object page: "The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values." The names reached it through the pipe, not as a parameter value: "You can pipe the objects to be sorted to this cmdlet."
Step 2: Add a Third Command
Another pipe sends the sorted names on to Select-Object:
'fig', 'banana', 'apple' | Sort-Object | Select-Object -First 2

apple
banana
Select-Object -First 2 kept the first two names it received and dropped the rest. The Select-Object page: "To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters." Because the commands run from left to right, the names were sorted first, so the first two were apple and banana.
Step 3: Sort by a Property of Each Object
What travels through a PowerShell pipe is objects, not just text. Each name is a string object with a Length property, its number of characters:
'fig', 'banana', 'apple' | Sort-Object -Property Length
fig
apple
banana

Sort-Object read the Length of each object it received: 3 letters for fig, 5 for apple and 6 for banana.
Step 4: See That the Pipe Sends One Object at a Time
Measure-Object counts the objects it receives. First, send the list through the pipe:
'fig', 'banana', 'apple' | Measure-Object
Count : 3
Average :
Sum :
Maximum :
Minimum :
StandardDeviation :
Property :
Then give the same list as the value of its -InputObject parameter:
Measure-Object -InputObject 'fig', 'banana', 'apple'
Count : 1
Average :
Sum :
Maximum :
Minimum :
StandardDeviation :
Property :
Through the pipe, Measure-Object counted 3 objects. As a parameter value, the whole list arrived as one object, and it counted 1. The about_Pipelines page: "When you pipe multiple objects to a command, PowerShell sends the objects to the command one at a time. When you use a command parameter, the objects are sent as a single array object."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Piping into a command that takes no pipeline input: 'fig' | Get-Location | The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. | Pipe into a command that has a parameter accepting pipeline input. The FAQ shows how to check |
Starting the next line with the pipe: 'fig', 'banana', 'apple', Enter, then | Sort-Object | The first line ran on its own and printed the names unsorted. The second failed with ParserError: An empty pipe element is not allowed. | End the first line with the pipe instead, as the FAQ shows |
The list as an argument: Sort-Object 'fig', 'banana', 'apple' | Nothing printed. The names went to the -Property parameter, which Get-Help Sort-Object -Parameter Property shows at Position? 0, and no objects came in to sort | Send the objects through the pipe |
FAQ
How do I check whether a command accepts pipeline input?
Ask Get-Help about the command's parameters. Get-Help Sort-Object -Parameter InputObject printed Accept pipeline input? true (ByValue). The about_Pipelines page: "To support pipelining, the receiving cmdlet must have a parameter that accepts pipeline input. Use the Get-Help command with the Full or Parameter options to determine which parameters of a cmdlet accept pipeline input."
Can a pipeline go over several lines?
Yes, when each line but the last ends with the pipe. Typing 'fig', 'banana', 'apple' | made the console show >> and wait; Sort-Object on the next line printed apple, banana, fig. The about_Pipelines page: "When a pipe operator is the last token on the line, the PowerShell parser joins the next line to current command to continue the construction of the pipeline."
Conclusion
'fig', 'banana', 'apple' | Sort-Object printed apple, banana, fig: the pipe sent the list's objects, one at a time, into the next command. Add a pipe for each further command, end a line with the pipe when a pipeline goes on, and pipe only into commands that accept pipeline input.
Additional Resources
- about_Pipelines — how pipelines work, pipeline input, and one-at-a-time processing.
- Sort-Object — sorting objects, by default or by a property.
- Select-Object — keeping the first, last or unique objects.
