PowerShell Lesson 5: CSV and Excel Data

📎 This article includes 3 downloadable practice files ↓

⏱ 1 min read

📘 PowerShell Beginner Course · Lesson 5 of 6

“`powershell
$orders = Import-Csv .\sales.csv | Select-Object *, @{ n = “amount”; e = { [int]$_.qty * [int]$_.rate } }
$summary = $orders | Group-Object region | ForEach-Object {
[pscustomobject]@{ region = $_.Name; orders = $_.Count; sales = ($_.Group | Measure-Object amount -Sum).Sum }
} | Sort-Object sales -Descending
$summary | Export-Csv region_summary.csv -NoTypeInformation
“`

Calculated properties (@{ n = ...; e = { ... } }) add columns; [pscustomobject] builds clean result rows.

Real Excel files

The community ImportExcel module (Install-Module ImportExcel -Scope CurrentUser) reads and writes .xlsx without Excel installed: Export-Excel report.xlsx -AutoSize -TableName Sales.

💡 Export-Csv in Windows PowerShell 5.1 writes UTF-16 by default in some versions; add -Encoding UTF8 so Excel and other tools read ₹ and Indian names correctly.

Practice

Run the script next to sales.csv and open the summary file it saves.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Reply

Your email address will not be published. Required fields are marked *