YouTip LogoYouTip

Powershell Pipeline Filtering

PowerShell Pipeline and Filtering |

\\n\\n

One of PowerShellโ€™s most powerful features is its pipeline mechanism.

\\n\\n

The 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\\n

This 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
\\n\\n

Oneใ€What is a pipeline?

\\n\\n

In PowerShell, the pipeline symbol | (vertical bar) indicates passing the output of the preceding command as input to the next command.

\\n\\n

Unlike traditional command-line text streams, the PowerShell pipeline passes objects, enabling more precise and powerful operations.

\\n\\n

Example: List running services and sort them

\\n\\n
Get-Service | Where-Object {$_.Status -eq "Running"} | Sort-Object DisplayName\\n
\\n\\n

Explanation:

\\n\\n
    \\n
  1. Get-Service retrieves all service objects;
  2. \\n
  3. Where-Object filters services whose status is "Running";
  4. \\n
  5. Sort-Object sorts the services by display name.
  6. \\n
\\n\\n
\\n\\n

Twoใ€Object filtering๏ผšWhere-Object

\\n\\n

Basic Syntax

\\n\\n
Where-Object { Conditional Expression }\\n
\\n\\n

Inside {}, use $_ to represent the current object in the pipeline.

\\n\\n

Example 1: Filter processes using more than 500 MB of memory

\\n\\n
Get-Process | Where-Object { $_.WorkingSet -gt 500MB }\\n
\\n\\n

Example 2: Filter services whose name starts with "Win"

\\n\\n
Get-Service | Where-Object { $_.Name -like "Win*" }\\n
\\n\\n
\\n\\n

Threeใ€Extracting properties๏ผšSelect-Object

\\n\\n

Select-Object extracts the fields you care about from objects, commonly used for simplified output, file redirection, or report generation.

\\n\\n

Example: List all servicesโ€™ names and statuses

\\n\\n
Get-Service | Select-Object Name, Status\\n
\\n\\n

You can also rename fields:

\\n\\n
Get-Service | Select-Object @{Name="Service Name"; Expression={$_.DisplayName}}, Status\\n
\\n\\n
\\n\\n

Fourใ€Sorting data๏ผšSort-Object

\\n\\n

Sort-Object sorts objects based on a specified property.

\\n\\n

Example 1: Sort processes by memory usage in descending order

\\n\\n
Get-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\\n

Example: Display service information in table format

\\n\\n
Get-Service | Format-Table -Property Name, Status, DisplayName\\n
\\n\\n

Output to a file:

\\n\\n
Get-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\\n

1. View all processes consuming CPU (sorted by CPU usage)

\\n\\n
Get-Process | Where-Object { $_.CPU -gt 0 } | Sort-Object CPU -Descending\\n
\\n\\n

2. Find all services whose display name contains "Time"

\\n\\n
Get-Service | Where-Object { $_.DisplayName -like "*Time*" }\\n
\\n\\n

3. Export disk information

\\n\\n
Get-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\\n

PowerShell 3.0 and later support shorthand syntax:

\\n\\n
Where-Object Name -like "*Win*"\\n
\\n\\n

Equivalent to:

\\n\\n
Where-Object { $_.Name -like "*Win*" }\\n
\\n\\n
\\n

Beginners are advised to first master the standard syntax, then use shorthand syntax later for improved efficiency.

\\n
\\n\\n
\\n\\n

8ใ€Overview of common pipeline operations

\\n\\n\\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n \\n
CommandFunction Description
Where-ObjectFilter objects based on conditions
Select-ObjectExtract specified properties
Sort-ObjectSort objects by property
Format-TableFormat output as a table
Out-FileWrite output to a text file
Export-CsvExport data to a CSV file
\\n\\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-Object is the core tool for data filtering.
  • \\n
  • Select-Object and Sort-Object enable 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
\\n\\n

10. Practice Tasks

\\n\\n

Task 1:Filter all services whose name starts with "W" and sort by name

\\n\\n
Get-Service | Where-Object { $_.Name -like "W*" } | Sort-Object Name\\n
\\n\\n

Task 2:List the top 10 processes with the highest memory usage

\\n\\n
Get-Process | Sort-Object WorkingSet -Descending | Select-Object Name, WorkingSet -First 10\\n
\\n\\n

Task 3:Export all IPv4 addresses on this machine to a file

\\n\\n
Get-NetIPAddress -AddressFamily IPv4 | Select-Object IPAddress | Out-File -FilePath "C:ip_list.txt"\\n
โ† Powershell Control StructuresCmdlet Process And Service Man โ†’