Using Functions inside the Pipeline

by Oct 17, 2011

Code inside PowerShell functions always is placed in the begin, process, or end block. You may not have noticed this because when you don't do this yourself, PowerShell places your entire function code inside an end block.

When you try and run a function inside the pipeline, it becomes important to manually place the code into the appropriate script block. Only code inside the process block is executed for each incoming pipeline element. If you leave the code in the end block, only the last pipeline data element will be executed. Here is a sample:

function Test-Pipeline {
  param(
    [Parameter(ValueFromPipeline=$true)]
    $Incoming
  )
  
  process {
    "Working with $incoming"
  }
}

Get-Process | Test-Pipeline

Twitter This Tip!
ReTweet this Tip!