Pipeline

Using the pipeline symbol “|”, multiple commands can be linked together.

Pipeline syntax

Pipeline element1[ | Pipeline element2][ | Pipeline element3]...
# The [] part is optional.

As you can see from this syntax, actually just one cmdlet is a pipeline.

Example

To be more specific, combine a cmdlet with the pipeline symbol “|” as shown below.

Get-Process | Select-Object Name

ForEach-Object

Use ForEach-Object for the sequential processing of the pipeline.

Alias of ForEach-Object

ForEach-Object has the following aliases.

foreach
%

Example

# Outputs the values from 1 to 3, each doubled.
PS> 1..3 | ForEach-Object {$_ * 2}
PS> 2
PS> 4
PS> 6

Automatic variables $_ and $PSItem

$_ is an automatic variable that indicates the current value being handled by the pipeline.
There is also $PSItem, which means the same as $_.

The three phases

There are three phases in the pipeline sequential processing: Begin, Process, and End.

# Output the sum of the values from 1 to 3.
PS> 1..3 | % {$sum=0} {$sum+=$_} {$sum}
PS> 6

If you just output the sum, you may not understand the three phases we talked about earlier.
Therefore, we will rewrite the code so that we can understand each phase.

# Output the sum of the values from 1 to 3.
PS> 1..3 | % {$sum=0;"Begin $sum $_”} {$sum+=$_;"Process $sum $_"} {"End $sum $_"}
PS> Begin 0 
PS> Process 1 1
PS> Process 3 2
PS> Process 6 3
PS> End 6 

An abbreviated way of writing ForEach-Object

Example1

PS> Get-Process | ForEach-Object {$_.ProcessName}

↓ Abbreviated way of writing

PS> Get-Process | ForEach-Object ProcessName

Example2

PS> String' | ForEach-Object {$_.ToUpper()}

↓ Abbreviated way of writing

PS> String' | ForEach-Object ToUpper
STRING
PS> ‘String' | ForEach-Object ToLower
string

Where-Object

To Filter an object, use Where-Object.

Alias for Where-Object

There are two aliases for Where-Object.

where
?

Abbreviated way to write in Where-Object.

Get-Process | where {$_.Name -like "*chrome"}

↓ Abbreviated way of writing

Get-Process | where Name -like "*chrome"

Select-Object

To select an object in the pipeline, use Select-Object.

PS> 1..10 | Select-Object -First 3
1
2
3
PS> 1..10 | Select-Object -Last 3
8
9
10

Sort-Object

To align objects in the pipeline, use Sort-Object.

PS> 1..10 | Sort-Object -Descending | Select-Object -First 3
10
9
8
PS> 1..10 | Sort-Object -Descending | Select-Object -Last 3
3
2
1

Measure-Object

To count objects, Measure-Object is useful.

PS> 1..10 | Measure-Object -Sum

Count             : 10
Average           : 
Sum               : 55
Maximum           : 
Minimum           : 
StandardDeviation : 
Property          : 

To get all the results, specify the AllStats parameter.

PS> 1..10 | Measure-Object -AllStats 

Count             : 10
Average           : 5.5
Sum               : 55
Maximum           : 10
Minimum           : 1
StandardDeviation : 3.02765035409749
Property  

YouTube

Click here for a video explanation.

About Pipelines

Last modified October 11, 2022: update (8216439)