PowerShell Working with the PowerShell pipeline


PowerShell introduces an object pipelining model, which allows you to send whole objects down through the pipeline to consuming commandlets or (at least) the output. In contrast to classical string-based pipelining, information in piped objects don't have to be on specific positions. Commandlets can declare to interact with Objects from the pipeline as input, while return values are sent to the pipeline automatically.


  • BEGIN The first block. Executed once at the beginning. The pipeline input here is $null, as it has not been set.
  • PROCESS The second block. Executed for each element of the pipeline. The pipeline parameter is equal to the currently processed element.
  • END Last block. Executed once at the end. The pipeline parameter is equal to the last element of the input, because it has not been changed since it was set.


In most cases, the input of the pipeline will be an array of objects. Although the behavior of the PROCESS{} block may seem similar to the foreach{} block, skipping an element in the array requires a different process.

If, like in foreach{}, you used continue inside the PROCESS{} block, it would break the pipeline, skipping all following statements including the END{} block. Instead, use return - it will only end the PROCESS{} block for the current element and move to the next.

In some cases, there is a need to output the result of functions with different encoding. The encoding of the output of the CmdLets is controlled by the $OutputEncoding variable. When the output is intended to be put into a pipeline to native applications, it might be a good idea to fix the encoding to match the target $OutputEncoding = [Console]::OutputEncoding

Additional references:

Blog article with more insight about $OutputEncoding https://blogs.msdn.microsoft.com/powershell/2006/12/11/outputencoding-to-the-rescue/