Powerful Piping in PowerShell: Chain Commands Together
The pipe (|) sends output from one command into another. This is what makes PowerShell powerful.
How It Works
The pipe (|) is a connector. Output from the left command becomes input to the right command. Instead of doing 3 separate steps, one pipeline does it all in one powerful line.
Think of it like an assembly line: each command does one job, then passes results to the next command.
Code Examples
Basic Pipeline
# Get files, send them to Sort-Object
Get-ChildItem | Sort-Object LastWriteTime
# Without piping, you'd need to:
# 1. Run Get-ChildItem
# 2. Look at results manually
# 3. Run Sort-Object with specific files
# With piping: one command does it all!
Chaining Multiple Commands
# Get files → Filter to .txt → Show only name → Sort
Get-ChildItem | Where-Object {$_.Name -like "*.txt"} | Select-Object Name | Sort-Object Name
# Each command passes results to the next:
# 1. Get-ChildItem finds all files
# 2. Where-Object filters to only .txt
# 3. Select-Object shows only Name column
# 4. Sort-Object alphabetizes them
Real-World Cleanup
# Find and delete old .tmp files
Get-ChildItem -Filter "*.tmp" | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item -WhatIf
# 1. Find .tmp files
# 2. Filter to older than 30 days
# 3. Delete (with -WhatIf to preview first!)
Display Specific Data
# Show top 5 processes by memory usage
Get-Process | Sort-Object Memory -Descending | Select-Object Name, Memory | Select-Object -First 5
# 1. Get all processes
# 2. Sort by memory (biggest first)
# 3. Show only name and memory
# 4. Limit to first 5
Most Used Options
- | - Pipe - sends output to next command
- Where-Object - Filter results by condition
- Sort-Object - Order results alphabetically or numerically
- Select-Object - Choose which properties to display
The Trick: Power Usage
Build pipelines step by step:
# Start simple
Get-ChildItem
# Add sorting
Get-ChildItem | Sort-Object LastWriteTime -Descending
# Add filtering
Get-ChildItem | Where-Object {$_.Length -gt 1MB} | Sort-Object LastWriteTime -Descending
# Add cleanup (show only columns we care about)
Get-ChildItem | Where-Object {$_.Length -gt 1MB} | Sort-Object LastWriteTime -Descending | Select-Object Name, Length
# Build complexity gradually - easier to debug!
Remember: Each | passes the entire result set to the next command. This is incredibly powerful!
Learn It Through Practice
Stop reading and start practicing right now:
The interactive environment lets you type these commands and see real results immediately.
Next in PowerShell for Beginners
This is part of the PowerShell for Beginners series:
- Getting Started - Your first commands
- Command Discovery - Find what exists
- Getting Help - Understand commands
- Working with Files - Copy, move, delete
- Filtering Data - Where-Object and Select-Object
- Pipelines - Chain commands together
Related Resources
Summary
You now understand:
- How this command works
- The most common ways to use it
- One powerful trick to level up
- Where to practice hands-on
Practice these examples until they feel natural. Then tackle the next command in the series.
Ready to practice? Head to the interactive environment and try these commands yourself. That's how it sticks!
What PowerShell commands confuse you? Drop it in the comments!
Top comments (0)