Skip to main content
SysAdmin Shell Scripting Essentials

Export-CSV PowerShell: Syntax, Flags, Exit Codes, Troubleshooting

What is powershell export to csv and when to use it?

powershell export to csv is the operation using Export-Csv to convert PowerShell objects into comma-separated variable strings and save them to a file. It supports type information, custom delimiters, append, and quoting.

Get-Process | Export-Csv -Path .Processes.csv -NoTypeInformation

This example exports running processes to a CSV file without the type header. The -NoTypeInformation flag suppresses the #TYPE line (default in PowerShell 6+).

Syntax

Export-Csv
    [[-Path] <String>]
    [[-Delimiter] <Char>]
    -InputObject <PSObject>
    [-Encoding <Encoding>]
    [-Force]
    [-NoClobber]
    [-Append]
    [-IncludeTypeInformation]
    [-NoTypeInformation]
    [-UseCulture]
    [-UseQuotes <QuoteKind>]
    [-QuoteFields <String[]>]
    [-LiteralPath <String>]
    [<CommonParameters>]

Tested on PowerShell 7.6 (Microsoft.PowerShell.Utility module). Official documentation at learn.microsoft.com.

Options and Flags

Flag Type Default Description
-Path String Current directory File path to save CSV output.
-Delimiter Char Comma (,) Character to delimit fields. Example: -Delimiter ';'.
-UseCulture Switch False Uses system locale’s list separator. Overrides -Delimiter.
-NoTypeInformation Switch True (PS 6+) Omits the #TYPE header line. Default in PowerShell Core.
-IncludeTypeInformation Switch False (PS 6+) Includes the #TYPE header line. Required for exact round-trip with Import-Csv on older PowerShell.
-Append Switch False Appends to existing CSV file. Does not add header if file exists and is not empty.
-Force Switch False Overwrites read-only files. With -Append, writes header even if file is non-empty.
-NoClobber Switch False Prevents overwriting an existing file; error if file exists.
-UseQuotes QuoteKind AsNeeded Controls when fields are quoted: Always, Never, AsNeeded (default).
-QuoteFields String[] None Specifies property names to always quote. Used with -UseQuotes AsNeeded.
-Encoding Encoding utf8NoBOM File encoding: ASCII, UTF8, UTF8BOM, UTF8NoBOM, BigEndianUnicode, etc.
See also  PowerShell Rename-Item: Syntax, Examples & Troubleshooting

Usage Examples

Example 1: Export running processes to CSV

Get-Process | Export-Csv -Path .Processes.csv -NoTypeInformation

Writes every property of each System.Diagnostics.Process object to Processes.csv. The -NoTypeInformation flag suppresses the #TYPE header (default in PS 6+). This file can be consumed by Excel or imported back via Import-Csv.

Example 2: Export with semicolon delimiter for European locales

Get-Process | Export-Csv -Path .Processes.csv -Delimiter ';' -NoTypeInformation

Many European regional settings use ; as the list separator. Using -Delimiter ';' ensures Excel opens the CSV correctly without needing to change system settings.

Example 3: Append to an existing CSV file

Get-Service | Select-Object Name,Status,DisplayName |
    Export-Csv -Path services.csv -NoTypeInformation -Append -Force

Appends service data to services.csv. The -Force parameter allows writing to a read-only file. If the file exists and is non-empty, no header is written. If the file is empty, a header is added. -Force with -Append writes a header even if the file already has content, which can create duplicate headers.

Troubleshooting & Common Errors

Error Message Root Cause Resolution Command
Access to the path '...' is denied. File is open in another process (e.g., Excel readonly). Close the file in the other application, then retry.
Could not find a part of the path '...'. Directory does not exist. New-Item -ItemType Directory -Path 'C:Exports' -Force
File '...' already exists. Use -Force or remove the file. -NoClobber is set. Remove -NoClobber or add -Force.
Unexpected column order or missing columns Select-Object not used before Export-Csv. Always pipe through Select-Object -Property Name,Status to control order.

Closing Tip

Always pipe objects through Select-Object before Export-Csv to control column order and limit the number of properties, avoiding unexpected schema changes due to computed properties or nested objects.

See also  Install wget on macOS — Verified Syntax, Flags & Troubleshooting

Frequently Asked Questions

What is the difference between Export-Csv and ConvertTo-Csv in PowerShell?

Answer: Export-Csv writes pipeline objects directly to a CSV file; ConvertTo-Csv converts objects to CSV strings and outputs them to the pipeline.

Use Export-Csv when you need to persist data to a file. Use ConvertTo-Csv when you need to further manipulate the CSV text or pipe it to other cmdlets. ConvertTo-Csv does not accept -Path or -Append.

Get-Process | Export-Csv -Path processes.csv -NoTypeInformation
Get-Process | ConvertTo-Csv -NoTypeInformation | Out-File -FilePath processes.csv -Encoding utf8

When should I use the -Append flag with Export-Csv?

Answer: Use -Append to add records to an existing CSV file without overwriting it; ideal for incremental logging or data collection over time.

Without -Append, Export-Csv replaces the file. With -Append, it appends rows and writes the header only if the file does not exist. Ensure column headers match or use -NoTypeInformation to avoid duplicate headers.

Get-Service | Export-Csv -Path services.csv -Append -NoTypeInformation

How do I fix “Export-Csv : Cannot open file” error due to locked CSV?

Answer: Close any application holding the file open (Excel, text editor), or add a retry loop and release handles.

The error occurs when another process has exclusive write access. For automation, implement a retry with Start-Sleep and Try/Catch until the file is free. Alternatively, export to a temp file and move it atomically.

$retry = 3; while ($retry--) { try { $data | Export-Csv output.csv -NoTypeInformation; break } catch { Start-Sleep -Seconds 1 } }

Does Export-Csv work on Linux, macOS, and Azure Cloud Shell?

Answer: Yes.

Confirm using $PSVersionTable.PSEdition – it must be ‘Core’ for cross-platform compatibility. File paths use forward slashes on Unix. Default encoding may differ (UTF8NoBOM on Unix vs UTF8BOM on Windows). Use -Encoding utf8 for consistency.

Get-ChildItem /tmp | Export-Csv -Path /tmp/items.csv -Encoding utf8 -NoTypeInformation

What is the fastest way to export a large dataset to CSV in PowerShell?

Answer: Use Export-Csv with -NoTypeInformation and -Encoding utf8 for streaming; for datasets exceeding 1 million rows, consider using a .NET StreamWriter to bypass PowerShell serialization overhead.

See also  chown Linux Command: Syntax, Recursive, Troubleshooting Guide

Export-Csv streams objects as they arrive, but object serialization overhead is significant for huge datasets. The .NET approach writes directly to disk with minimal memory use.

$stream = [System.IO.StreamWriter]::new('big.csv')
$stream.WriteLine('Name,Id')
$data | ForEach-Object { $stream.WriteLine("$($_.Name),$($_.Id)") }
$stream.Close()