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
-Forceif overwriting
Issue: Special characters corrupted
- Solution: Use
-Encoding UTF8
Issue: Headers misaligned
- Solution: Ensure consistent property names
Related Articles
CSV Operations
- PowerShell Import-CSV - Import CSV files
- ConvertTo-CSV - Convert to CSV format
- ConvertFrom-CSV - Convert from CSV format
Data Selection & Filtering
- PowerShell Select-Object - Select columns before export
- PowerShell Where-Object - Filter rows before export
- PowerShell ForEach-Object - Process data
- PowerShell Measure-Object - Analyze exported data
Data Structures
- PowerShell Arrays - Work with exported arrays
- PowerShell Hashtables - Create complex data structures
- PowerShell Variables - Store export data
Output & File Operations
- PowerShell Output to File - Alternative file output
- PowerShell Get-Content - Read exported files
- PowerShell Format Table - Format before export
- PowerShell Output Table - Display data as tables
Control Flow & Logic
- PowerShell If-Else Statement - Conditional exports
- PowerShell Switch Statement - Switch-based logic
- PowerShell Try-Catch - Error handling
String Operations
- PowerShell Strings - String manipulation in exports
- PowerShell Replace Strings - Clean data before export
- PowerShell DateTime Format - Format dates in exports
Data Sources
- PowerShell Get-Process - Export process data
- PowerShell Get-Service - Export service data
- PowerShell List Files - Export file listings
- PowerShell Get File Properties - Export file metadata
- PowerShell Get-ADUser - Export AD user data
Functions & Organization
- PowerShell Functions - Create export functions
- PowerShell Add-Member - Add properties to export
Sorting & Grouping
- PowerShell Sort-Object - Sort before export
- PowerShell Group-Object - Group data before export
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell reference
- Complete PowerShell Tutorial - Comprehensive course
- Active Directory Guide - AD data export
PowerShell doesn’t have a built-in parameter -NoHeader in Export-CSV cmdlet, hence we use the ConvertTo-Csv cmdlet in PowerShell.