PowerShell Pipeline and Filtering
One of PowerShell's most powerful features is the 'pipeline' mechanism.
The pipeline lets you combine multiple commands like building blocks, with each step processing and passing objects, which makes scripts cleaner and greatly improves the flexibility and efficiency of data processing.
This section introduces the basic principle of the pipeline |, the two ways of filtering, object property extraction, and common combination techniques, helping you write more powerful and smarter command-line operations.
I. What Is the Pipeline?
In PowerShell,The pipe symbol |(the vertical bar) means 'passing' the output of the previous command as input to the next command.
Unlike the text stream in traditional command lines, what is passed in the PowerShell pipeline is object, which makes operations more precise and powerful.
Example: List running services and sort them
Get-Service | Where-Object {$_.Status -eq "Running"} | Sort-Object DisplayName
Explanation:
Get-ServiceGet all service objects;Where-ObjectFilter services whose status is Running;Sort-ObjectSort by service name.
II. Object Filtering: Where-Object
Basic Syntax
Where-Object { 条件表达式 }
in {} Inside it, use $_ to represent each object in the current pipeline.
Example 1: Filter processes using more than 500MB of memory
Get-Process | Where-Object { $_.WorkingSet -gt 500MB }
Example 2: Filter services whose name starts with 'Win'
Get-Service | Where-Object { $_.Name -like "Win*" }
III. Extracting Properties: Select-Object
Select-Object Used to extract the fields you care about from objects; commonly used for concise output, redirecting to a file, or building reports.
Example: List the name and status of all services
Get-Service | Select-Object Name, Status
You can also rename fields:
Get-Service | Select-Object @{Name="Service name"; Expression={$_.DisplayName}}, Status
IV. Sorting Data: Sort-Object
Sort-Object Used to sort objects by a certain property.
Example 1: Sort processes in descending order by memory usage
Get-Process | Sort-Object WorkingSet -Descending | Select-Object Name, WorkingSet -First 5
V. Output and Formatting: Format-Table, Out-File, etc.
Example: Display service information in table form
Get-Service | Format-Table -Property Name, Status, DisplayName
Output to file:
Get-Service | Where-Object {$_.Status -eq "Running"} |
Select-Object Name, DisplayName |
Out-File -FilePath "C:\RunningServices.txt"
VI. Common Usage Combinations (Hands-on)
1. View all processes consuming CPU (sorted by CPU usage)
Get-Process | Where-Object { $_.CPU -gt 0 } | Sort-Object CPU -Descending
2. Find all services whose name contains "Time"
Get-Service | Where-Object { $_.DisplayName -like "*Time*" }
3. Export hard disk information
Get-CimInstance Win32_LogicalDisk |
Select-Object DeviceID, VolumeName, Size, FreeSpace |
Export-Csv -Path "C:\diskinfo.csv" -NoTypeInformation
VII. Where-Object Shorthand Syntax (PowerShell 3.0+)
PowerShell 3.0 and later support shorthand syntax:
Where-Object Name -like "*Win*"
Equivalent to:
Where-Object { $_.Name -like "*Win*" }
Beginners are advised to first learn the standard syntax, then use the shorthand later to improve efficiency.
VIII. Overview of Common Pipeline Operation Commands
| Command | Description |
|---|---|
Where-Object |
Conditionally filter objects |
Select-Object |
Extract specified properties |
Sort-Object |
Sort by property |
Format-Table |
Format output as a table |
Out-File |
Write output to a text file |
Export-Csv |
Export data to a CSV file |
IX. Summary
- PowerShell pipelines are based on object passing, which is more powerful than traditional text pipelines
Where-Objectand is the core tool for data filteringSelect-ObjectandSort-Objectcapable of flexibly extracting, sorting, and organizing data- combining multiple commands can accomplish complex data processing tasks and is the foundation of system automation
X. Practice Tasks
**Task 1:** Filter all services starting with "W" and sort by name
Get-Service | Where-Object { $_.Name -like "W*" } | Sort-Object Name
**Task 2:** List the top 10 processes by memory usage
Get-Process | Sort-Object WorkingSet -Descending | Select-Object Name, WorkingSet -First 10
**Task 3:** Export all local IPv4 addresses to a file
Get-NetIPAddress -AddressFamily IPv4 | Select-Object IPAddress | Out-File -FilePath "C:\ip_list.txt"