Pipeline Input¶
Pipeline input allows a function to receive values directly from the pipeline, just like built‑in cmdlets (Get-Process, Select-Object, Where-Object, etc.). This is one of PowerShell’s most powerful features because it lets your functions participate naturally in pipeline‑based workflows.
To enable pipeline input, you use the [Parameter()] attribute with one of these options:
ValueFromPipeline = $trueValueFromPipelineByPropertyName = $true
Each mode behaves differently and is explained below.
1. ValueFromPipeline¶
This mode binds the entire incoming object to a parameter.
Syntax:
param(
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
1.1 Example: receiving pipeline values¶
function Show-Item {
param(
[Parameter(ValueFromPipeline = $true)]
$Item
)
"Item: $Item"
}
"apple","banana","cherry" | Show-Item
Explanation:
- Each pipeline value (
"apple","banana","cherry") is passed to the function one at a time. $Itemreceives each value.- The function runs once per pipeline object.
- Output:
Item: apple
Item: banana
Item: cherry
1.2 How the function executes¶
When a function accepts pipeline input:
- PowerShell does not call the function once
- It calls the function once per pipeline element
- The parameter marked with
ValueFromPipelinereceives each element
This is identical to how cmdlets behave.
2. ValueFromPipelineByPropertyName¶
This mode binds pipeline input by matching property names.
Syntax:
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
$Name
)
PowerShell looks for a property on the incoming object with the same name as the parameter.
2.1 Example: binding by property name¶
function Show-Name {
param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
$Name
)
"Name: $Name"
}
$objects = @(
[pscustomobject]@{ Name = "Alice"; Age = 30 }
[pscustomobject]@{ Name = "Bob"; Age = 25 }
)
$objects | Show-Name
Explanation:
- Each object has a property called
Name. - The parameter
$Namematches that property. - PowerShell binds the property value to the parameter.
- Output:
Name: Alice
Name: Bob
2.2 What happens if the property doesn’t exist?¶
If the incoming object does not have a matching property:
- The parameter receives
$null - The function still runs
Example:
"hello" | Show-Name
Output:
Name:
Because "hello" has no .Name property.
3. Combining Both Modes¶
A parameter can accept both direct pipeline input and property‑name binding:
param(
[Parameter(ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
$Item
)
This allows the function to handle:
- Raw values
- Objects with matching properties
4. Functions With Multiple Pipeline Parameters¶
Only one parameter can receive pipeline input per pipeline object.
If multiple parameters are marked as pipeline‑enabled, PowerShell binds:
ValueFromPipelinefirst- Then
ValueFromPipelineByPropertyName
Example:
function Test {
param(
[Parameter(ValueFromPipeline = $true)]
$Value,
[Parameter(ValueFromPipelineByPropertyName = $true)]
$Name
)
"Value: $Value | Name: $Name"
}
Calling:
[pscustomobject]@{ Name = "Alice"; Value = 10 } | Test
Output:
Value: 10 | Name: Alice
5. Pipeline Input With Processing Blocks¶
Advanced functions use:
begin {}process {}end {}
Pipeline input is processed inside process {}.
Example: processing pipeline input properly¶
function Show-Item {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true)]
$Item
)
begin { "Starting..." }
process { "Item: $Item" }
end { "Done." }
}
"one","two","three" | Show-Item
Output:
Starting...
Item: one
Item: two
Item: three
Done.
Explanation:
beginruns once before pipeline processingprocessruns once per pipeline objectendruns once after all input is processed
6. Why Pipeline Input Matters¶
Pipeline‑enabled functions:
- Integrate naturally with PowerShell’s pipeline model
- Allow chaining commands together
- Make scripts more modular and reusable
- Behave like built‑in cmdlets
- Improve readability and reduce temporary variables
Pipeline input is one of the defining features of PowerShell’s design.