0
0
PowerShellscripting~5 mins

Pipeline input (ValueFromPipeline) in PowerShell

Choose your learning style9 modes available
Introduction

Pipeline input lets you send data from one command to another easily. It helps connect commands so they work together smoothly.

You want to send a list of files from one command to another to process each file.
You need to filter data with one command and then use the filtered data in another command.
You want to automate a task that works on many items one by one without writing loops.
You want to build reusable commands that accept input from other commands.
You want to chain commands to create a simple workflow.
Syntax
PowerShell
param(
  [Parameter(ValueFromPipeline=$true)]
  [string]$InputValue
)

process {
  # Code to handle $InputValue
}

ValueFromPipeline=$true means the parameter accepts input from the pipeline.

The process block runs once for each item passed through the pipeline.

Examples
This script greets each name passed through the pipeline.
PowerShell
param(
  [Parameter(ValueFromPipeline=$true)]
  [string]$Name
)

process {
  Write-Output "Hello, $Name!"
}
This script doubles each number received from the pipeline.
PowerShell
param(
  [Parameter(ValueFromPipeline=$true)]
  [int]$Number
)

process {
  Write-Output ($Number * 2)
}
Sample Program

This function takes names from the pipeline and greets each one.

PowerShell
function Greet-User {
  param(
    [Parameter(ValueFromPipeline=$true)]
    [string]$UserName
  )

  process {
    Write-Output "Hello, $UserName!"
  }
}

# Send names through the pipeline
"Alice", "Bob", "Charlie" | Greet-User
OutputSuccess
Important Notes

Remember to use the process block to handle each pipeline item.

Only parameters marked with ValueFromPipeline=$true can accept pipeline input.

Pipeline input helps avoid writing loops manually.

Summary

Pipeline input lets commands pass data easily to each other.

Use ValueFromPipeline=$true to accept pipeline input in parameters.

The process block runs once per pipeline item.