Create correctly formatted CSV in PowerShell
How to produce clean CSV and TSV files in PowerShell with Export-Csv and ConvertTo-Csv: delimiters, quoting, encoding, version differences and why Out-File is the wrong tool.
PowerShell can produce CSV files that spreadsheets, databases and other scripts read cleanly. That only works when you use cmdlets that serialize objects into delimited text, not cmdlets that write console display output. This review covers what the conversion changes, which settings decide whether the file is usable, and when plain text or another format is a better choice.
Short verdict
Export-Csv and ConvertTo-Csv are the right choice for tabular data built from objects, such as process lists, account exports or report rows. Their main benefit is that quoting and escaping are handled for you, so commas, quotes or line breaks inside values do not shift columns. The decisive tradeoff is that CSV is flat. Every value becomes text, type information and nested structure are lost, and several defaults differ between Windows PowerShell 5.1 and PowerShell 7. Prefer plain text when a person will read the output in a console, message or log. Prefer a structured format such as JSON when the data is hierarchical.
What CSV conversion actually changes
Both cmdlets take each object in the pipeline and write its property names as the header row. Each object's property values become one row, separated by a delimiter, which is a comma by default. Values are converted to strings. ConvertTo-Csv returns the lines as strings in the pipeline, and Export-Csv writes them to a file.
Get-Process |
Select-Object Name, Id, CPU |
Export-Csv -Path .\processes.csv -NoTypeInformation
Using Select-Object first lets you choose the columns and keep the data flat. The columns are taken from the first object in the pipeline. If later objects have properties the first one lacks, those properties are not exported, so normalize mixed objects with Select-Object before exporting.
Separate what the file stores from what a spreadsheet displays. The file holds plain text. Dropped leading zeros, reformatted dates and long numbers shown in scientific notation come from the application that opens the file, not from PowerShell. Check the raw file with Get-Content or a text editor before deciding the export is wrong.
A property that holds an array or a nested object is written as its type name, for example System.Object[], rather than its contents. Flatten such values yourself, for example by joining them with a separator that differs from the delimiter.
$items |
Select-Object Name, @{Name = 'Tags'; Expression = { $_.Tags -join ';' }} |
Export-Csv -Path .\items.csv -NoTypeInformation
Why Out-File and Format-Table give the wrong result
Out-File writes what you would see in the console: columns aligned with spaces and sometimes truncated. It has no delimiter parameter, and naming the file with a .csv extension does not make its contents CSV. Piping Format-Table into Export-Csv also fails, because it exports formatting objects instead of your data. Use format cmdlets only at the end of a pipeline, and only for display.
If you need Out-File for its own options, convert to CSV first and let Out-File write the resulting lines:
Get-Service |
Select-Object Name, Status |
ConvertTo-Csv -NoTypeInformation |
Out-File -FilePath .\services.csv -Encoding utf8
Choosing a delimiter
The -Delimiter parameter takes a single character. For tab-separated output, pass a tab using PowerShell's backtick escape inside double quotes. Semicolons are common where the comma is the decimal separator. The -UseCulture switch uses the current culture's list separator. That is convenient on one machine, but the output then depends on the regional settings of whoever runs the script. For files shared across systems, set the delimiter explicitly.
# Tab-separated
$rows | Export-Csv -Path .\data.tsv -Delimiter "`t" -NoTypeInformation
# Semicolon-separated
$rows | Export-Csv -Path .\data.csv -Delimiter ';' -NoTypeInformation
Import-Csv must be given the same delimiter when reading the file back. Otherwise each line is read as a single column.
Version differences that affect the file
| Behavior | Windows PowerShell 5.1 | PowerShell 7 |
| Type header line | A #TYPE line is written by default; suppress it with -NoTypeInformation | Not written by default; add it with -IncludeTypeInformation |
| Quoting | Every field is quoted | Every field is quoted by default; -UseQuotes (Always, AsNeeded, Never) and -QuoteFields control this |
| Default encoding | Not UTF-8; specify -Encoding when data contains non-ASCII characters | UTF-8 without a byte order mark |
Use -UseQuotes Never with care. Any value that contains the delimiter, a quote or a line break will then break the row. If you want minimal quoting, AsNeeded is the safer option.
For scripts that must run on both versions, pass -NoTypeInformation and -Encoding explicitly. PowerShell 7 still accepts -NoTypeInformation, so the same command produces consistent output on both.
Will the file stay usable after copying or sharing?
CSV is plain text with no hidden presentation layer. Copying its contents preserves the characters, and the reading order is simply row by row. The practical risks lie in how the receiving application interprets the file: how it detects encoding, which delimiter it expects, and what automatic formatting it applies.
- Open the raw file in a text editor to confirm the header, delimiter and quoting.
- Round-trip the file with Import-Csv and compare row counts and column names with the source data.
- If non-ASCII text appears garbled in a spreadsheet, suspect encoding first. UTF-8 with a byte order mark is often used for spreadsheet compatibility, but confirm this with the recipient's software.
- Warn recipients about codes with leading zeros or long numeric IDs, or ask them to import those columns as text.
- When using -Append, keep the same columns as the existing file. Export-Csv reports an error on mismatched properties unless -Force is used, and forcing it can produce misaligned data.
No evidence was supplied about how screen readers or other assistive technology handle CSV in particular applications. Table navigation depends on the program that opens the file. If the audience will read the information rather than process it, provide a plain-text summary or a formatted document alongside the CSV.
Evidence gaps
This review is based on documented cmdlet behavior. No test results were supplied for specific spreadsheet versions, import tools or assistive technologies. Before relying on a file in production, test it against the exact application and settings that will consume it.