PowerShell Pipeline and Filtering |
\\n\\nOne of PowerShellโs most powerful features is its pipeline mechanism.
\\n\\nThe pipeline allows you to combine multiple commands like building blocks, where each step processes and passes objects along. This not only makes scripts more concise, but also significantly enhances data processing flexibility and efficiency.
\\n\\nThis section introduces the fundamental principles of the pipeline |, two filtering approaches, object property extraction, and common combination techniquesโhelping you write more powerful and intelligent command-line operations.
\\n\\n
OneใWhat is a pipeline?
\\n\\nIn PowerShell, the pipeline symbol | (vertical bar) indicates passing the output of the preceding command as input to the next command.
Unlike traditional command-line text streams, the PowerShell pipeline passes objects, enabling more precise and powerful operations.
\\n\\nExample: List running services and sort them
\\n\\nGet-Service | Where-Object {$_.Status -eq "Running"} | Sort-Object DisplayName\\n\\n\\nExplanation:
\\n\\n- \\n
Get-Serviceretrieves all service objects; \\n Where-Objectfilters services whose status is "Running"; \\n Sort-Objectsorts the services by display name. \\n
\\n\\n
TwoใObject filtering๏ผWhere-Object
\\n\\nBasic Syntax
\\n\\nWhere-Object { Conditional Expression }\\n\\n\\nInside {}, use $_ to represent the current object in the pipeline.
Example 1: Filter processes using more than 500 MB of memory
\\n\\nGet-Process | Where-Object { $_.WorkingSet -gt 500MB }\\n\\n\\nExample 2: Filter services whose name starts with "Win"
\\n\\nGet-Service | Where-Object { $_.Name -like "Win*" }\\n\\n\\n\\n\\n
ThreeใExtracting properties๏ผSelect-Object
\\n\\nSelect-Object extracts the fields you care about from objects, commonly used for simplified output, file redirection, or report generation.
Example: List all servicesโ names and statuses
\\n\\nGet-Service | Select-Object Name, Status\\n\\n\\nYou can also rename fields:
\\n\\nGet-Service | Select-Object @{Name="Service Name"; Expression={$_.DisplayName}}, Status\\n\\n\\n\\n\\n
FourใSorting data๏ผSort-Object
\\n\\nSort-Object sorts objects based on a specified property.
Example 1: Sort processes by memory usage in descending order
\\n\\nGet-Process | Sort-Object WorkingSet -Descending | Select-Object Name, WorkingSet -First 5\\n\\n\\n\\n\\n
FiveใOutput and formatting๏ผFormat-TableใOut-File etc.
\\n\\nExample: Display service information in table format
\\n\\nGet-Service | Format-Table -Property Name, Status, DisplayName\\n\\n\\nOutput to a file:
\\n\\nGet-Service | Where-Object {$_.Status -eq "Running"} | Select-Object Name, DisplayName | Out-File -FilePath "C:RunningServices.txt"\\n\\n\\n\\n\\n
6. Common Usage Combinations (Practical Examples)
\\n\\n1. View all processes consuming CPU (sorted by CPU usage)
\\n\\nGet-Process | Where-Object { $_.CPU -gt 0 } | Sort-Object CPU -Descending\\n\\n\\n2. Find all services whose display name contains "Time"
\\n\\nGet-Service | Where-Object { $_.DisplayName -like "*Time*" }\\n\\n\\n3. Export disk information
\\n\\nGet-CimInstance Win32_LogicalDisk | Select-Object DeviceID, VolumeName, Size, FreeSpace | Export-Csv -Path "C:diskinfo.csv" -NoTypeInformation\\n\\n\\n\\n\\n
7. Where-Object Shorthand (PowerShell 3.0+๏ผ
\\n\\nPowerShell 3.0 and later support shorthand syntax:
\\n\\nWhere-Object Name -like "*Win*"\\n\\n\\nEquivalent to:
\\n\\nWhere-Object { $_.Name -like "*Win*" }\\n\\n\\n\\n\\n\\nBeginners are advised to first master the standard syntax, then use shorthand syntax later for improved efficiency.
\\n
\\n\\n
8ใOverview of common pipeline operations
\\n\\n| Command | \\nFunction Description | \\n
|---|---|
Where-Object | \\n Filter objects based on conditions | \\n
Select-Object | \\n Extract specified properties | \\n
Sort-Object | \\n Sort objects by property | \\n
Format-Table | \\n Format output as a table | \\n
Out-File | \\n Write output to a text file | \\n
Export-Csv | \\n Export data to a CSV file | \\n
\\n\\n
9. Summary
\\n\\n- \\n
- PowerShell pipelines are based on object passing, making them more powerful than traditional text-based pipelines. \\n
Where-Objectis the core tool for data filtering. \\n Select-ObjectandSort-Objectenable flexible extraction, sorting, and organization of data. \\n - Combining multiple commands enables complex data processing tasksโforming the foundation of system automation. \\n
\\n\\n
10. Practice Tasks
\\n\\nTask 1:Filter all services whose name starts with "W" and sort by name
\\n\\nGet-Service | Where-Object { $_.Name -like "W*" } | Sort-Object Name\\n\\n\\nTask 2:List the top 10 processes with the highest memory usage
\\n\\nGet-Process | Sort-Object WorkingSet -Descending | Select-Object Name, WorkingSet -First 10\\n\\n\\nTask 3:Export all IPv4 addresses on this machine to a file
\\n\\nGet-NetIPAddress -AddressFamily IPv4 | Select-Object IPAddress | Out-File -FilePath "C:ip_list.txt"\\n
YouTip