Tramite il modulo ImportExcel di PowerShell è possibile gestire un file Excel.

Install

Come prima cosa va importato il modulo

PowerShell: Import Module Admin

Install-Module -Name ImportExcel
oppure per installarlo solo per l'utente corrente

PowerShell: Import Module CurrentUser

Install-Module -Name ImportExcel -Scope CurrentUser

Leggere un file excel

Una delle possibilità è leggere un file Excel come questo contenente una tabella
table.xlsx
table.xlsx
con questo semplice codice

PowerShell: Read-Excel

Import-Excel -Path "$pwd\Table.xlsx"
da un risultato come questo

Text: output

ID Comune 
-- ------ 
 1 Milano 
 2 Torino 
 3 Bologna
 4 Roma   
ovvero ritorna una lista di oggetti dove le proprietà sono i nomi delle colonne.

Quindi si può creare un PowerShell che cicla sulle righe e legge le singole colonne

PowerShell

Import-Module ImportExcel

$rows = Import-Excel -Path "$pwd\Table.xlsx" -WorksheetName "Sheet2"

$ciIT = [System.Globalization.CultureInfo]::GetCultureInfo('it-IT')

$rows | ForEach-Object {
    $id = $_.ID
    $comune = $_.Comune
    $data = $_.'Esempio Data'
    $numero = $_.'Es. Numero'

    # Write-Output "ID: $id, Comune: $comune, Data: $data, Number: $numero"
    # Write-Output "ID: $id, Comune: $comune, Data: $($data.ToString('yyyy-MM-dd')), Number: $($numero.ToString('#,##0.00'))"
    Write-Output "ID: $id, Comune: $comune, Data: $($data.ToString($ciIT)), Number: $($numero.ToString($ciIT))"
}
da questo risultato

Text: output

ID: 10, Comune: Milano, Data: 01/01/2024 00:00:00, Number: 3,45
ID: 20, Comune: Torino, Data: 15/09/2023 00:00:00, Number: 123,456
ID: 30, Comune: Bologna, Data: 31/10/2023 00:00:00, Number: 78
ID: 40, Comune: Roma, Data: 04/12/2022 00:00:00, Number: 551234,5
Tags:
PowerShell199 Excel11 Esempi224
Potrebbe interessarti anche: