Search notes:

PowerShell functions: begin, process and end blocks

A PowerShell function can contain a begin, process and end block each of which contains a sequence of statements.
These blocks are invoked when the respective function participates in a pipeline.
The begin block is invoked before the first object within the pipeline is processed by the function. Similarly, the end block is invoked after the last object has been processed.
The process block is invoked for each object as it is processed by the function.
Within the process block, the automatic variable $input refers to the objects in the pipeline. If there is no process block, the $input variable is an array in the end block that contains all objects that were passed from the pipeline. This is because the process block removes the object from the $input variable.
The begin,process and end blocks are equivalent to the BeginProcessing(), ProcessRecord() and EndProcessing() methods in derived classes from System.Management.Automation.Cmdlet of compiled cmdlets.
If none of the blocks is explicitly defined, the function's script block is called as end-process-block (but see filter for a way to make a function have a process block only).

Simple demonstration

The following simple PowerShell script tries to demonstrate the basic functionalities of these blocks.
Two functions are defined, one with all three blocks, and another where the process block is missing.
After defining the functions, two pipelines are created where some objects are piped into the two functions. It can be seen that the $input variable is filled in the function without the process block, but is empty in the function with the process block.
#
#  Define a function with all three blocks:
#  begin, process and end
#
function show-beginProcessEnd {
  begin   { "begin:   input = $input" }
  process { "process: input = $input" }
  end     { "end:     input = $input" }
}

#
#  Define another function that does not have
#  the process block
#
function show-beginEnd {
  begin   { "begin:   input = $input" }
  end     { "end:     input = $input" }
}

#
#  Pipe a few objects into the first function
#
1 .. 4 | show-beginProcessEnd
#
#  begin:   input =
#  process: input = 1
#  process: input = 2
#  process: input = 3
#  process: input = 4
#  end:     input =

#
#  Pipe a few objects into the second function
#
1 .. 4 | show-beginEnd
#
#  begin:   input =
#  end:     input = 1 2 3 4
Github repository about-PowerShell, path: /language/function/begin-process-end-block/with-without-process.ps1

Function to sum up the elements of an array

With these blocks, it's possible to create a function into which the elements of an array can be piped and then sums up the individual elements of that array:
function sum {
   begin   { $s  = 0  }
   process { $s += $_ }
   end     { $s       }
}

7, 3, 9, 1, 22 | sum
#
#  42
Github repository about-PowerShell, path: /language/function/begin-process-end-block/sum.ps1

See also

Parameters with their valueFromPipeline attribute set to $true.

Index