Wiki di Michele

La pipeline e gli oggetti

GuidaUltima modifica: 24/10/2026 Verificata il 24/10/2026

Il cuore di PowerShell: i comandi si collegano con | passandosi oggetti, non testo. Con pochi cmdlet si filtrano, ordinano e trasformano i dati.

Where-Object (filtrare)

Get-Process | Where-Object CPU -gt 100
Get-Process | Where-Object { $_.CPU -gt 100 -and $_.Name -like "chrome*" }
Get-Service | Where-Object Status -eq 'Running'
Get-ChildItem | Where-Object { $_.Length -gt 1MB }
Get-Process | ? Name -match '^(chrome|firefox)$'

$_ rappresenta l'oggetto corrente. La sintassi semplificata (Where-Object Proprietà -operatore valore) vale per un solo confronto.

Operatori di confronto

OperatoreSignificato
-eq, -neUguale, diverso
-gt, -geMaggiore, maggiore o uguale
-lt, -leMinore, minore o uguale
-like, -notlikeCon caratteri jolly (*, ?)
-match, -notmatchEspressione regolare
-contains, -inAppartenenza a una collezione
-and, -or, -notOperatori logici

I confronti ignorano maiuscole/minuscole; per distinguerle: -ceq, -clike, -cmatch.

Select-Object (scegliere proprietà)

Get-Process | Select-Object Name, Id, CPU
Get-Process | Select-Object -First 5
Get-Process | Select-Object -Last 5
Get-Process | Select-Object -Skip 10 -First 5
Get-Process | Select-Object -ExpandProperty Name
Get-Process | Select-Object -Unique Name
Get-Process | Select-Object Name, @{Name='MemMB'; Expression={[math]::Round($_.WS/1MB,1)}}

Sort-Object (ordinare)

Get-Process | Sort-Object CPU -Descending
Get-Process | Sort-Object Name, Id
Get-Service | Sort-Object Status, DisplayName

ForEach-Object (ripetere)

1..5 | ForEach-Object { $_ * 2 }
Get-ChildItem *.txt | ForEach-Object { $_.Name.ToUpper() }
Get-Process | ForEach-Object { "$($_.Name): $($_.Id)" }
Get-Content .\computer.txt | ForEach-Object { Test-Connection $_ -Count 1 -Quiet }

Group-Object e Measure-Object

Get-Process | Group-Object Company
Get-ChildItem -Recurse | Group-Object Extension | Sort-Object Count -Descending
Get-Process | Measure-Object
Get-Process | Measure-Object CPU -Sum -Average -Maximum -Minimum
Get-ChildItem -File | Measure-Object Length -Sum

Altri cmdlet di pipeline

Get-Process | Select-Object -First 3 | Format-Table
Get-Process | Format-List Name, Id
Get-Service | Out-GridView
Get-Process | Out-String
Get-Process | Tee-Object -FilePath .\p.txt
CmdletUso
Format-Table / Format-ListPresentazione a tabella / a elenco (solo per l'ultimo passaggio)
Out-GridViewFinestra interattiva filtrabile
Tee-ObjectSalva e inoltra l'output
Out-NullScarta l'output

Non mettere Format-Table o Format-List prima di altri cmdlet: producono oggetti di formattazione, non dati utilizzabili.

Esempi pratici

I 5 processi che usano più memoria:

Get-Process | Sort-Object WorkingSet -Descending | Select-Object -First 5 Name, @{n='MB';e={[math]::Round($_.WorkingSet/1MB)}}

Servizi automatici fermi:

Get-Service | Where-Object { $_.StartType -eq 'Automatic' -and $_.Status -ne 'Running' }

Contare i file per estensione:

Get-ChildItem -Recurse -File | Group-Object Extension | Sort-Object Count -Descending | Select-Object -First 10 Count, Name

Confronto con la pipeline testuale

In CMD o Linux si elaborano stringhe (findstr, awk). In PowerShell si accede direttamente alle proprietà ($_.Status, $_.Length): niente parsing di colonne.

Tag

comandipowershellwindows

Altre pubblicazioni in questa sezione

Suggerisci una correzione