-
2.1 The Pipeline
-
# Pass objects from one cmdlet to the next
-
Get-Process | Where-Object { $_.CPU -gt 10 }
-
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
-
Get-Service | Where-Object Status -eq ‘Running’ | Measure-Object
-
# Chaining — everything stays as objects
-
Get-ChildItem C:\Windows\System32 -Filter *.dll |
-
Where-Object Length -gt 1MB |
-
Sort-Object Length -Descending |
-
Select-Object Name, @{N=’SizeMB’;E={[math]::Round($_.Length/1MB,2)}} |
-
Format-Table -AutoSize
-
2.2 Working With Objects
-
# Select specific properties
-
Get-Process | Select-Object Name, Id, CPU, WorkingSet
-
# Computed / calculated properties
-
Get-Process | Select-Object Name, @{ Name = ‘MemoryMB’ Expression = { [math]::Round($_.WorkingSet / 1MB, 2) } }
-
# Where-Object filtering Get-Process | Where-Object { $_.Name -like ‘chrome*’ -and $_.CPU -gt 5 }
-
# ForEach-Object — do something to each object Get-Process | ForEach-Object { “Process: $($_.Name) uses $([math]::Round($_.WorkingSet/1MB,1)) MB” }
-
2.3 Sorting, Grouping & Measuring
-
# Sort
-
Get-Process | Sort-Object CPU -Descending
-
Get-Process | Sort-Object Name, CPU
-
# Group
-
Get-Process | Group-Object Name | Sort-Object Count -Descending
-
# Measure
-
Get-Process | Measure-Object CPU -Sum -Average -Maximum
-
Get-ChildItem C:\ -Recurse -File | Measure-Object Length -Sum
-
2.4 Formatting Output
-
# Format-Table (default) Get-Process | Format-Table Name, CPU, WorkingSet -AutoSize
-
# Format-List (one property per line) Get-Process -Name explorer | Format-List *
-
# Format-Wide Get-Process | Format-Wide Name -Column 4
-
# Out-GridView — interactive GUI table (Windows) Get-Process | Out-GridView -Title ‘Running Processes’ -PassThru | Stop-Process
-
# Export
-
Get-Process | Export-Csv -Path processes.csv -NoTypeInformation
-
Get-Process | ConvertTo-Json | Out-File processes.json