Code Reference

Export CSV Report

Powershell · Example / how-to

Export CSV Report

Powershell · Example / how-to


📋 Overview

Query objects, project columns, and export a CSV report with Export-Csv and UTF-8 encoding.

🔧 Core concepts

PieceRole
Select-ObjectChoose / rename columns
Export-CsvWrite spreadsheet-friendly file
-NoTypeInformationCleaner CSV header
PipelineCompose filters

💡 Examples

export_csv_report.ps1:

param(
    [string]$OutFile = ".\report.csv",
    [int]$Top = 50
)

$ErrorActionPreference = "Stop"

$rows = Get-ChildItem -Path $PSScriptRoot -File -Recurse -ErrorAction SilentlyContinue |
    Where-Object { $_.Extension -in ".md", ".ps1", ".ts", ".py" } |
    Sort-Object Length -Descending |
    Select-Object -First $Top @{
        Name = "RelativePath"
        Expression = { $_.FullName.Substring($PSScriptRoot.Length).TrimStart("\") }
    }, Length, LastWriteTime, Extension

$rows | Export-Csv -Path $OutFile -NoTypeInformation -Encoding utf8

Write-Host "Wrote $($rows.Count) rows to $OutFile"

From process list:

Get-Process |
  Select-Object Name, Id, CPU, WorkingSet |
  Export-Csv .\processes.csv -NoTypeInformation -Encoding utf8

⚠️ Pitfalls

  • Default encoding can confuse Excel — prefer -Encoding utf8.
  • Nested objects become strings poorly — flatten with calculated properties.
  • Export-Csv overwrites by default; confirm the path in scripts.

On this page