Skip to main content

PowerShell Export-CSV: Complete Guide with Headers, Encoding & Examples [2024]

• 3 min read
powershell export-csv no-header csv data export file operations

Master PowerShell Export-CSV—write objects to CSV files efficiently. This guide covers basic export, headers, encoding, append operations, and production patterns.

Quick Syntax

<object> | Export-Csv -Path "C:\output.csv" -NoTypeInformation

Basic Export

Simple Export

Get-Process | Select-Object Name, Id, Memory |
    Export-Csv -Path "C:\processes.csv" -NoTypeInformation

Export with Specific Properties

Get-Service | Select-Object Name, Status, StartType |
    Export-Csv -Path "C:\services.csv" -NoTypeInformation

Headers

With Headers (Default)

$users = @(
    [PSCustomObject]@{Name="John"; Age=30; City="NYC"},
    [PSCustomObject]@{Name="Jane"; Age=28; City="LA"}
)

$users | Export-Csv -Path "users.csv" -NoTypeInformation
# Output: Name,Age,City
#         John,30,NYC
#         Jane,28,LA

No Headers

$users | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 |
    Set-Content -Path "users.csv"
# Output: John,30,NYC
#         Jane,28,LA

Custom Headers

$data | ForEach-Object {
    [PSCustomObject]@{
        ID = $_.Id
        "Full Name" = $_.Name
        "E-mail" = $_.Email
    }
} | Export-Csv -Path "output.csv" -NoTypeInformation

Encoding Options

UTF-8 (Default)

Get-Service | Export-Csv -Path "services.csv" -Encoding UTF8 -NoTypeInformation

UTF-8 Without BOM

Get-Service | Export-Csv -Path "services.csv" -Encoding UTF8NoBOM -NoTypeInformation

ASCII

Get-Service | Export-Csv -Path "services.csv" -Encoding ASCII -NoTypeInformation

Append Mode

Add to Existing CSV

# First export
Get-Process | Select-Object Name, Id | Export-Csv -Path "procs.csv" -NoTypeInformation

# Append more data
Get-Service | Select-Object Name, Status |
    ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 |
    Add-Content -Path "procs.csv"

Overwrite vs Append

Overwrite (Default)

# Replaces entire file
Get-Process | Export-Csv -Path "data.csv" -NoTypeInformation

Prevent Overwrite

# Error if file exists
Get-Process | Export-Csv -Path "data.csv" -NoClobber -NoTypeInformation

Formatting

Format Before Export

Get-Process |
    Select-Object @{Name="ProcessName"; Expression={$_.Name}},
                  @{Name="MemoryMB"; Expression={[Math]::Round($_.Memory/1MB, 2)}},
                  @{Name="Threads"; Expression={$_.Threads.Count}} |
    Export-Csv -Path "report.csv" -NoTypeInformation

Delimiter Options

Different Delimiters

# Tab-delimited
Get-Process | Export-Csv -Path "data.tsv" -Delimiter "`t" -NoTypeInformation

# Semicolon
Get-Process | Export-Csv -Path "data.csv" -Delimiter ";" -NoTypeInformation

# Custom
Get-Process | ConvertTo-Csv -Delimiter "|" -NoTypeInformation |
    Set-Content -Path "data.txt"

Real-World Examples

Export Active Directory Users

Get-ADUser -Filter * |
    Select-Object Name, SamAccountName, EmailAddress, Department |
    Export-Csv -Path "C:\ADUsers_$(Get-Date -Format 'yyyyMMdd').csv" -NoTypeInformation

Export Filtered Services

Get-Service |
    Where-Object { $_.Status -eq "Running" } |
    Select-Object Name, DisplayName, StartType |
    Export-Csv -Path "running_services.csv" -NoTypeInformation

Export with Calculations

Get-ChildItem -Path "C:\Logs" -File |
    Select-Object Name,
                  @{Name="SizeMB"; Expression={[Math]::Round($_.Length/1MB, 2)}},
                  @{Name="AgeHours"; Expression={[Math]::Round(((Get-Date) - $_.LastWriteTime).TotalHours)}} |
    Export-Csv -Path "log_analysis.csv" -NoTypeInformation

Error Handling

Handle Export Errors

try {
    Get-Process | Export-Csv -Path "C:\output.csv" -ErrorAction Stop
    Write-Output "Export successful"
} catch {
    Write-Error "Export failed: $_"
}

Create Directory if Missing

$outputDir = "C:\Reports"
if (-not (Test-Path $outputDir)) {
    New-Item -ItemType Directory -Path $outputDir | Out-Null
}

Get-Process | Export-Csv -Path "$outputDir\processes.csv" -NoTypeInformation

Performance Tips

  • Use -NoTypeInformation (smaller file)
  • Filter data BEFORE export
  • Use Select-Object to choose columns
  • Export in batches for large datasets

Common Issues

Issue: File locked after export

  • Solution: Close Excel, use -Force if overwriting

Issue: Special characters corrupted

  • Solution: Use -Encoding UTF8

Issue: Headers misaligned

  • Solution: Ensure consistent property names

CSV Operations

  • PowerShell Import-CSV - Import CSV files
  • ConvertTo-CSV - Convert to CSV format
  • ConvertFrom-CSV - Convert from CSV format

Data Selection & Filtering

Data Structures

Output & File Operations

Control Flow & Logic

String Operations

Data Sources

Functions & Organization

Sorting & Grouping

  • PowerShell Sort-Object - Sort before export
  • PowerShell Group-Object - Group data before export

Comprehensive Guides

PowerShell doesn’t have a built-in parameter -NoHeader in Export-CSV cmdlet, hence we use the ConvertTo-Csv cmdlet in PowerShell.