Basic Pipeline Support in Functions
suggest changeThis is an example of a function with the simplest possible support for pipelining.
Any function with pipeline support must have at least one parameter with the ParameterAttribute ValueFromPipeline or ValueFromPipelineByPropertyName set, as shown below.
function Write-FromPipeline {
param(
[Parameter(ValueFromPipeline)] # This sets the ParameterAttribute
[String]$Input
)
Write-Host $Input
}
$foo = 'Hello World!'
$foo | Write-FromPipeline
Output:
Hello World!
Note: Without a process block, the function body runs once as an end block, so a ValueFromPipeline parameter receives only the last pipeline object. This example pipes a single string, so that is enough. In Windows PowerShell 3.0 and later, parameter-attribute switches default to $true. In earlier versions, you must specify ValueFromPipeline=$true. Do not name a parameter $input; that name is an automatic variable.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents