02 / WORKING WITH DATA · LESSON 06

Pass it down the pipeline

Send objects from one command into another.

10 min · learn at your pace

The vertical bar connects commands

The pipe character, |, passes output from the command on its left into the command on its right. PowerShell passes objects, preserving their types and properties.

1, 2, 3 | Measure-Object -Sum

Measure-Object receives three numbers and returns a measurement object. Its Sum property is 6, and its Count property is 3.

Read a pipeline from left to right

$measurement = 1, 2, 3 | Measure-Object -Sum
$measurement.Sum

First the numbers enter the pipeline. Next the measurement cmdlet calculates a result. Finally that result is assigned to a variable. You can inspect it just like any other object.

Pipelines let you compose small, understandable steps. Later you will filter, choose properties, and sort before sending the final objects to a file.

Formatting cmdlets such as Format-Table are for display. Keep them at the end of a display pipeline; use the original objects when you still need to calculate or export.

Try an example.ps1
1, 2, 3 | Measure-Object -Sum
YOUR TURN

Put it into practice.

Send 3, 7, 2, and 9 through Measure-Object -Sum, then output only the numeric total.

See an explained solution
One way to solve it.ps1
$numbers = 3, 7, 2, 9
$total = $numbers | Measure-Object -Sum
$total.Sum

The pipeline carries four numeric values into Measure-Object. Reading .Sum returns 21 without the surrounding measurement properties.