PowerShell Pipelines

Pipeline patterns with ForEach-Object, Where-Object, and pipeline-aware parameter binding.

Pipelines Quick Reference

Filter with Where-Object
Get-Service | Where-Object Status -eq Running
Get-Process | Where-Object { $_.CPU -gt 100 }
Get-ADUser -Filter * -Properties LastLogonDate | Where-Object { $_.LastLogonDate -lt (Get-Date).AddDays(-90) }
Select properties
Get-Process | Select-Object Name, CPU, WorkingSet
Get-Process | Select-Object -First 10
Get-Process | Select-Object -Property Name, @{N='MB';E={[math]::Round($_.WorkingSet/1MB)}}
Sort
Get-Process | Sort-Object CPU -Descending
Get-Service | Sort-Object Status, DisplayName
Group and measure
Get-Service | Group-Object Status | Select-Object Name, Count
Get-ChildItem *.log | Measure-Object -Property Length -Sum -Average
ForEach-Object
Get-Service | ForEach-Object { "$($_.Name): $($_.Status)" }
1..10 | ForEach-Object { $_ * $_ }
Get-Content servers.txt | ForEach-Object { Test-Connection $_ -Count 1 -Quiet }
Pipeline to file
Get-Process | Export-Csv procs.csv -NoTypeInformation
Get-EventLog System -Newest 100 | Out-File events.txt
Get-Service | ConvertTo-Json | Set-Content services.json

See Also

  • Basics — foundational cmdlet and pipe syntax

  • PSObjects — object properties flowing through pipelines