The ability to accept pipeline input has been included in PowerShell since Version 1.0. There are three main ways to deal with the pipeline: $input
, filters, and (fully specified) functions.
The $input
automatic variable exposes an enumerator (think collection) of all the values passed in on the pipeline. An example using the $input
automatic variable might look like this:
Function get-pipelineinput{ $input | Foreach-Object {Write-Host "the object was $_"} }
The second option, filters, are simply functions whose bodies are executed for each pipeline element. Filters use the $_
symbol to represent the current pipeline element. Here is an example of a filter script:
Filter get-reverse{ $_.ToString().Reverse() }
Because there is no way to specify what types of values were available for pipeline input using the $input
or $_
variables, these are not good solutions for most production scripts. With PowerShell Version 2.0 and the introduction of CmdletBinding()
, another more...