0
0
PowershellHow-ToBeginner · 3 min read

How to Use Pipeline in PowerShell: Syntax and Examples

In PowerShell, the | symbol is used to create a pipeline that passes the output of one command as input to the next. This allows chaining commands to process data step-by-step in a simple and readable way.
📐

Syntax

The basic syntax of a PowerShell pipeline uses the | character to connect commands. Each command processes the data and passes it along.

  • Command1 | Command2 | Command3: Output of Command1 goes to Command2, then Command2's output goes to Command3.
  • Each command receives objects, not just text, allowing powerful data manipulation.
powershell
Get-Process | Where-Object { $_.CPU -gt 100 } | Sort-Object CPU -Descending
💻

Example

This example shows how to list running processes that use more than 100 CPU units, then sort them by CPU usage in descending order.

powershell
Get-Process | Where-Object { $_.CPU -gt 100 } | Sort-Object CPU -Descending | Select-Object -Property Id, ProcessName, CPU
Output
Id ProcessName CPU -- ----------- --- 1234 chrome 250.5 5678 powershell 150.2
⚠️

Common Pitfalls

Common mistakes when using pipelines include:

  • Using commands that output text instead of objects, which can break the pipeline.
  • Forgetting to use $_ to refer to the current object in script blocks.
  • Assuming output is text rather than structured objects, leading to unexpected results.

Always check if commands output objects and use Where-Object or ForEach-Object properly.

powershell
Get-Process | Where-Object { $_.CPU -gt 100 }  # Correct usage

Get-Process | Where-Object { $_.CPU -gt 100 }      # Incorrect, missing $_
📊

Quick Reference

PowerShell pipeline tips:

  • Use | to chain commands.
  • Use $_ inside script blocks to refer to the current object.
  • Commands pass objects, not just text.
  • Use Where-Object to filter objects.
  • Use ForEach-Object to perform actions on each object.

Key Takeaways

Use the pipeline symbol | to pass output from one command to another in PowerShell.
PowerShell pipelines pass objects, enabling powerful and flexible data processing.
Inside script blocks, use $_ to refer to the current object in the pipeline.
Common errors include forgetting $_ or using commands that output plain text instead of objects.
Use Where-Object and ForEach-Object to filter and manipulate pipeline data.