Wiki di Michele

Get-Content, Set-Content, Select-String

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

Leggere, scrivere e cercare nel contenuto dei file di testo.

Get-Content (cat, type)

Get-Content .\file.txt
Get-Content .\file.txt -TotalCount 10
Get-Content .\file.txt -Tail 20
Get-Content .\file.log -Wait
Get-Content .\file.log -Tail 10 -Wait
Get-Content .\file.txt -Raw
Get-Content .\file.txt -Encoding UTF8
ParametroSignificato
-TotalCount NSolo le prime N righe (come head)
-Tail NSolo le ultime N righe (come tail)
-WaitResta in attesa di nuove righe (come tail -f); interrompi con Ctrl+C
-RawRestituisce un'unica stringa invece di un array di righe
-EncodingCodifica del file

Senza -Raw il risultato è un array con una riga per elemento: conviene per elaborare riga per riga.

Set-Content, Add-Content, Out-File

Set-Content .\file.txt "Prima riga"
Set-Content .\file.txt -Value "riga1","riga2"
Add-Content .\file.txt "Riga aggiunta"
"Testo" | Out-File .\file.txt
"Testo" | Out-File .\file.txt -Append
Get-Process | Out-File .\processi.txt
Clear-Content .\file.txt
CmdletComportamento
Set-ContentScrive sovrascrivendo
Add-ContentAggiunge in coda
Out-FileScrive l'output formattato (con -Append aggiunge)
Clear-ContentSvuota il file senza eliminarlo

Anche gli operatori > e >> funzionano, ma in Windows PowerShell 5.1 salvano in UTF-16: meglio -Encoding utf8 con Out-File o Set-Content.

Modificare il contenuto

(Get-Content .\config.txt) -replace 'vecchio','nuovo' | Set-Content .\config.txt
(Get-Content .\config.txt -Raw) -replace '(?m)^#.*\r?\n','' | Set-Content .\config.txt

Racchiudere Get-Content tra parentesi fa leggere tutto il file prima di riscriverlo: indispensabile per scrivere sullo stesso file.

Select-String (grep)

Select-String -Path .\log.txt -Pattern "errore"
Select-String -Path .\*.log -Pattern "errore"
Select-String -Path .\log.txt -Pattern "errore" -CaseSensitive
Select-String -Path .\log.txt -Pattern "errore" -Context 2,2
Select-String -Path .\log.txt -Pattern "debug" -NotMatch
Select-String -Path .\*.txt -Pattern "pass" -List
Select-String -Path .\log.txt -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches
ParametroSignificato
-PatternTesto o espressione regolare (di default ignora maiuscole/minuscole)
-CaseSensitiveDistingue maiuscole e minuscole
-SimpleMatchCerca testo letterale, senza regex
-Context a,bRighe prima e dopo
-NotMatchRighe che NON corrispondono
-ListSolo la prima corrispondenza per file
-AllMatchesTutte le corrispondenze nella riga

Cercare ricorsivamente:

Get-ChildItem -Recurse -Filter *.php | Select-String "mysqli_connect"
Get-ChildItem -Recurse -File | Select-String "password" | Select-Object Path, LineNumber, Line

Estrarre solo la parte corrispondente (ad esempio gli indirizzi IP):

Select-String -Path .\log.txt -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches |
  ForEach-Object { $_.Matches.Value } | Sort-Object -Unique

Contare, ordinare, rimuovere duplicati

(Get-Content .\file.txt).Count
Get-Content .\file.txt | Sort-Object
Get-Content .\file.txt | Sort-Object -Unique
Get-Content .\file.txt | Measure-Object -Line -Word -Character

Leggere file strutturati

Get-Content .\dati.json | ConvertFrom-Json
Import-Csv .\dati.csv
[xml](Get-Content .\dati.xml)

Tag

comandipowershellwindows

Altre pubblicazioni in questa sezione

Suggerisci una correzione