Export PowerShell objects and variables to CSV

How Export-Csv turns PowerShell objects and variables into CSV or TSV: format options, common pitfalls, encoding, and when plain text works better.

Export-Csv is the built-in PowerShell cmdlet for writing objects to a comma-separated (or otherwise delimited) text file. It is the right tool when you want structured, tabular data that a spreadsheet, database import or another script can read. It does not change how text looks; it turns object properties into rows and columns. This review explains what that conversion does, where the output format can surprise you, and how to export the contents of a variable without losing meaning.

Is Export-Csv suitable for your use?

Use Export-Csv when the data is a collection of records with consistent, flat properties, such as processes, services, users or custom objects you build yourself. The main benefit is that every property becomes a named column, so the file stays machine-readable and can be re-imported with Import-Csv or opened in a spreadsheet.

The decisive tradeoff is that CSV is flat and untyped. Nested objects, arrays and rich types do not survive the trip: they are written as text, and anything read back with Import-Csv comes in as strings. If you need to preserve structure or types, a format such as JSON (ConvertTo-Json) or PowerShell's own CLIXML (Export-Clixml) is the better fit.

Plain text is preferable when the goal is a human-readable report, a log line or a single value. Out-File or Set-Content with formatted output is simpler there, and a CSV wrapper adds quotes and headers that a reader does not need.

What Export-Csv actually changes

Export-Csv serializes objects. For each object passed to it, it writes one line, and it builds a header row from property names. The stored characters in the file are the property values converted to strings, separated by a delimiter and, by default, wrapped in double quotes. Nothing is reversed, mirrored or restyled; the transformation is purely structural, from objects in memory to delimited text on disk.

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

Selecting properties before exporting is the most reliable way to control which columns appear and in what order.

Format details that differ by version

Behavior | Windows PowerShell 5.1 | PowerShell 6 and later
Type information line (#TYPE ...) at top of file | Written unless you add -NoTypeInformation | Omitted by default; add -IncludeTypeInformation to write it
Default encoding | Not UTF-8 without BOM; set -Encoding explicitly for non-ASCII text | UTF-8 without a byte order mark
Quote control | Every field is quoted | -UseQuotes (Always, AsNeeded, Never) and -QuoteFields are available

Because defaults changed between versions, scripts that must run in both should pass -NoTypeInformation and an explicit -Encoding rather than relying on defaults. Run Get-Help Export-Csv -Full on your own system to confirm the parameters your version supports.

Delimiters, CSV versus TSV

The -Delimiter parameter replaces the comma with another character. A tab produces TSV, which is often easier to paste into spreadsheets and avoids conflicts with commas inside values. -UseCulture instead uses the list separator of the current culture, which helps in regions where the semicolon is standard, but makes the output depend on the machine that produced it.

$data | Export-Csv -Path .\data.tsv -Delimiter "`t" -NoTypeInformation

Header comes from the first object

Columns are determined by the first object Export-Csv receives. If later objects have extra properties, those values are dropped; if they lack a property, the field is left empty. When exporting mixed objects, normalize them with Select-Object and a fixed property list first.

Exporting a variable to CSV

Piping a variable works the same as piping command output, as long as the variable holds objects with useful properties:

$users = Get-LocalUser | Select-Object Name, Enabled, LastLogon
$users | Export-Csv -Path .\users.csv -NoTypeInformation

The common surprises come from variables that hold something other than records:

  • An array of strings exports a single Length column, because the only property of a string is its length. Wrap each value in an object first.
  • A hashtable may export the hashtable's own properties (such as Keys, Values and Count) instead of its entries, depending on version. Convert it with [pscustomobject] so each key becomes a column.
  • Output from Format-Table or Format-List exports formatting objects, not your data. Never pipe Format-* output to Export-Csv; use Select-Object instead.
  • Properties holding arrays or nested objects are written as a type name such as System.Object[]. Flatten them, for example by joining an array into one string.
# Array of strings -> one named column
$names = 'alpha', 'beta', 'gamma'
$names | ForEach-Object { [pscustomobject]@{ Name = $_ } } |
    Export-Csv -Path .\names.csv -NoTypeInformation

# Hashtable -> one row with a column per key
$settings = @{ Server = 'app01'; Port = 8080 }
[pscustomobject]$settings | Export-Csv -Path .\settings.csv -NoTypeInformation

# Array property -> single joined field
Get-Service | Select-Object Name,
    @{ Name = 'DependsOn'; Expression = { ($_.ServicesDependedOn.Name) -join ';' } } |
    Export-Csv -Path .\services.csv -NoTypeInformation

To add rows to an existing file, use -Append. The appended objects should have the same properties as the existing header; otherwise columns can misalign or values can be dropped. -NoClobber prevents overwriting an existing file, and -Force allows writing to read-only files.

Will the CSV stay readable and usable after sharing?

CSV is plain text, so copying, emailing or versioning the file does not alter it. Problems usually arise when the receiving program interprets it.

  • Encoding: if values contain non-ASCII characters, mismatched encoding can turn them into garbled characters. Some spreadsheet programs rely on a byte order mark to detect UTF-8; PowerShell 6 and later offer a utf8BOM option. Behavior varies by program and version, so test with the actual destination.
  • Type loss: Import-Csv returns every value as a string, so dates and numbers must be converted again if a script needs them as typed values.
  • Spreadsheet reinterpretation: spreadsheets may reformat values that look like dates or long numbers, and may treat cells beginning with =, +, - or @ as formulas. If files containing untrusted input will be opened in a spreadsheet, sanitize those values before export.
  • Delimiter mismatch: a semicolon-separated file opened by software expecting commas will collapse into one column, and vice versa.
  • Reading order and assistive technology: in a text editor, a screen reader announces raw delimiters and quotes; opened in a spreadsheet, the same data is presented as a grid. No specific assistive technology was tested for this review, so check the destination if accessibility matters.

For copying data rather than saving a file, ConvertTo-Csv produces the same text as strings in the pipeline, which you can send to the clipboard. A tab delimiter typically pastes into separate spreadsheet cells, but confirm this in your target application.

$data | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation | Set-Clipboard

A plain-text equivalent is the better choice when the recipient will read the result rather than process it, such as in a chat message or ticket. Out-String or a short formatted table is easier to scan than quoted, delimited fields.

Gaps in this review

No measurements, test files or destination-specific results were supplied for this article. The behavior described above reflects PowerShell's documented cmdlet behavior and general CSV principles. How a particular spreadsheet, database importer or screen reader handles a given file was not verified and should be checked with a sample export before you rely on it.