Esportare e importare dati: CSV, JSON, XML, HTML
PowerShell lavora con oggetti e li converte in formati di scambio con pochi cmdlet.
CSV
Get-Process | Select-Object Name, Id, CPU | Export-Csv .\processi.csv -NoTypeInformation
Get-Process | Export-Csv .\processi.csv -NoTypeInformation -Encoding UTF8
Import-Csv .\processi.csv
Import-Csv .\dati.csv -Delimiter ";"
Import-Csv .\dati.csv | Where-Object Eta -gt 30
Import-Csv .\dati.csv | ForEach-Object { "$($_.Nome) $($_.Cognome)" }
| Parametro | Significato |
|---|---|
-NoTypeInformation | Evita la riga #TYPE iniziale (non serve in PowerShell 6+) |
-Delimiter | Separatore (Excel in italiano usa ;) |
-Encoding | Codifica; per Excel va bene UTF8 |
-UseCulture | Usa il separatore delle impostazioni regionali |
-Append | Aggiunge righe a un CSV esistente |
Esportare per Excel in italiano:
Get-Service | Select-Object Name, Status | Export-Csv .\servizi.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
Tutti i valori di un CSV importato sono stringhe: per confronti numerici converti con [int]$_.Eta.
JSON
Get-Process | Select-Object -First 3 Name, Id | ConvertTo-Json
Get-Process | Select-Object -First 3 Name, Id | ConvertTo-Json | Set-Content .\p.json
Get-Content .\p.json -Raw | ConvertFrom-Json
(Get-Content .\config.json -Raw | ConvertFrom-Json).database.host
Per oggetti annidati, aumenta la profondità (il valore predefinito è 2):
$obj | ConvertTo-Json -Depth 10
Modificare un file JSON:
$cfg = Get-Content .\config.json -Raw | ConvertFrom-Json
$cfg.porta = 8080
$cfg | ConvertTo-Json -Depth 10 | Set-Content .\config.json -Encoding UTF8
XML
Get-Process | Export-Clixml .\processi.xml
Import-Clixml .\processi.xml
[xml]$xml = Get-Content .\dati.xml
$xml.radice.elemento
Select-Xml -Path .\dati.xml -XPath "//utente[@id='1']"
Export-Clixml serializza oggetti PowerShell conservandone il tipo (ottimo per salvare credenziali cifrate con DPAPI sullo stesso utente e computer).
Get-Credential | Export-Clixml .\cred.xml
$cred = Import-Clixml .\cred.xml
HTML
Get-Service | Select-Object Name, Status | ConvertTo-Html -Title "Servizi" | Out-File .\servizi.html
Get-Service | ConvertTo-Html -Property Name, Status -PreContent "<h1>Servizi</h1>" | Out-File .\servizi.html
Invoke-Item .\servizi.html
Testo e tabelle
Get-Process | Out-File .\p.txt
Get-Process | Format-Table -AutoSize | Out-File .\p.txt -Width 200
Get-Process | Out-String
Get-Process | Out-GridView
Appunti
Get-Process | Select-Object -First 5 | Set-Clipboard
Get-Clipboard
Excel
Senza Excel installato, il modo più semplice è esportare in CSV. Con il modulo ImportExcel (da PowerShell Gallery):
Install-Module ImportExcel -Scope CurrentUser
Get-Process | Export-Excel .\processi.xlsx -AutoSize -TableName Processi
Import-Excel .\processi.xlsx
Confronto tra formati
| Formato | Quando usarlo |
|---|---|
| CSV | Tabelle piatte, scambio con Excel |
| JSON | Dati annidati, API, configurazioni |
| CLIXML | Salvare e ripristinare oggetti PowerShell |
| XML | Documenti strutturati, configurazioni legacy |
| HTML | Report leggibili nel browser |