The content of the process block runs once for each value received from the pipeline. The built-in $_ variable may be used to access objects in the pipeline within the process block:
function Show-Pipeline {
begin {
$position = $myinvocation.PipelinePosition
Write-Host "Pipeline position ${position}: Start"
}
process {
Write-Host "Pipeline position ${position}: $_"
$_
}
}
When an object is passed to the pipeline, the start message will be shown before the numeric value:
PS> $result = 1..2 | Show-Pipeline
Pipeline position 1: Start
Pipeline position 1: 1
Pipeline position 1: 2
Adding Show-Pipeline to the end of the pipeline will show that begin executes twice before process runs:
PS> $result = 1..2 | Show-Pipeline | Show-Pipeline
Pipeline position 1: Start
Pipeline position 2: Start
Pipeline position 1: 1
Pipeline position 2: 1
Pipeline position 1: 2
Pipeline position 2: 2
The $result variable will contain the output of the last Show-Pipeline command.