Work with CSV files in PowerShell

A practical review of PowerShell's CSV cmdlets: what Import-Csv and Export-Csv change, version and encoding differences, TSV handling, and when CSV is the wrong format.

PowerShell has built-in cmdlets that turn delimited text into objects and objects back into delimited text. That makes CSV and TSV files easy to filter, reshape and convert from the command line. The conversion is not neutral, though. It changes data types, quoting, encoding and sometimes which columns survive. This review explains what the cmdlets do, where the result stays reliable, and when another format is the better choice.

Evidence note: no test results, version-specific measurements or application-specific checks were supplied for this article. The behavior described here comes from the cmdlets' documented design. Where behavior depends on your PowerShell version or on the application that opens the file, the article says so rather than presenting one outcome as universal.

Is CSV conversion in PowerShell suitable for your use?

CSV fits flat, tabular data: one record per row, one simple value per column. Exports of users, inventory lists, log summaries and report data meant for spreadsheets or other tools all work well. The main benefit is interchange. Almost every spreadsheet, database import tool and scripting language can read a well-formed CSV file, and PowerShell can produce one from any object pipeline in a single command.

The decisive tradeoff is that CSV keeps only text. Numbers, dates and booleans are written as strings and read back as strings. Nested objects and arrays are flattened into a single text value unless you handle them yourself. If you need structure or types to survive a round trip, CSV is the wrong container.

  • Choose CSV or TSV for flat tables that other tools or people will open in a spreadsheet.
  • Choose JSON (ConvertTo-Json and ConvertFrom-Json) for nested or hierarchical data.
  • Choose Export-Clixml and Import-Clixml when the file only needs to come back into PowerShell with types intact.
  • Choose plain text output, such as Format-Table piped to Out-File, when the result is a human-readable report that no program will parse. Do not use that text as a data file.

Where the CSV capability lives and how to find it

The CSV cmdlets are part of the Microsoft.PowerShell.Utility module, which ships with both Windows PowerShell 5.1 and PowerShell 7. You do not need to install anything. If you are searching for help, use the cmdlet names rather than general phrases like "PowerShell spreadsheet".

Cmdlet | Input | Output | Typical use
Import-Csv | A file on disk | Objects, one per row | Read a CSV or TSV file for filtering or processing
Export-Csv | Objects from the pipeline | A file on disk | Save results as CSV or TSV
ConvertFrom-Csv | Strings in the pipeline | Objects | Parse CSV text from a variable, web response or command output
ConvertTo-Csv | Objects | Strings | Produce CSV text without writing a file

To confirm what your installed version supports, list the commands and read the full help locally:

Get-Command -Noun Csv
Get-Help Export-Csv -Full
$PSVersionTable.PSVersion

Check the version first. Several defaults described below differ between Windows PowerShell 5.1 and PowerShell 7.

What the conversion actually changes

CSV conversion is a change to the stored characters, not a visual effect. There is no presentation layer and no styling. When PowerShell reads a file, it turns each row into an object whose property names come from the header line. When it writes a file, it turns each object's properties into a header line and one delimited row per object. Each of the following points changes the data itself.

Every value becomes a string

Import-Csv returns every field as text. A column of quantities sorts alphabetically unless you cast it, so a value of 10 lands before a value of 9. Cast inside the expression when the type matters:

Import-Csv -Path ./orders.csv |
    Sort-Object { [int]$_.Quantity } -Descending

Dates need the same care. Parsing a date string depends on its format and on the current culture, so check a sample of rows before you trust date comparisons.

Columns come from the first object

Export-Csv builds its header from the properties of the first object it receives. Properties that appear only on later objects are not written. This can drop data silently when a pipeline mixes object shapes. One workaround is to collect every property name first and select them all explicitly:

$props = $items | ForEach-Object { $_.PSObject.Properties.Name } | Select-Object -Unique
$items | Select-Object -Property $props | Export-Csv -Path ./all-columns.csv -NoTypeInformation

Nested values are flattened to text

A property that holds an array or another object is written using its string representation. For an array, that is often a type name such as System.Object[] instead of the contents. To keep the information readable, flatten it yourself with a calculated property before you export:

$items | Select-Object Name,
    @{ Name = 'Tags'; Expression = { $_.Tags -join ';' } } |
    Export-Csv -Path ./items.csv -NoTypeInformation

Formatted output is not data

Piping Format-Table or Format-List into Export-Csv writes formatting instruction objects, not your data. Apply Select-Object to choose columns, and export before any Format-* cmdlet.

Version-dependent defaults

  • Type header: Windows PowerShell 5.1 adds a #TYPE line at the top of the file unless you pass -NoTypeInformation. PowerShell 7 omits it by default and adds it only with -IncludeTypeInformation. Passing -NoTypeInformation is harmless in both versions, so scripts meant for either version can include it.
  • Quoting: Windows PowerShell 5.1 wraps every field in double quotes. PowerShell 7 adds -UseQuotes (Always, AsNeeded or Never) and -QuoteFields, which let you control quoting. Never can produce ambiguous output if a value contains the delimiter.
  • Encoding: PowerShell 7 writes UTF-8 without a byte order mark by default. Windows PowerShell 5.1 uses a different default. Pass -Encoding explicitly when non-ASCII characters matter, so the result does not depend on the version that runs the script.

Converting between CSV and TSV

The -Delimiter parameter works on all four cmdlets. In PowerShell, a tab is written as a backtick followed by t inside double quotes. Because the cmdlets handle quoting during the conversion, a field that contains the target delimiter stays a single field, as long as quoting is not disabled:

# TSV to CSV
Import-Csv -Path ./data.tsv -Delimiter "`t" |
    Export-Csv -Path ./data.csv -NoTypeInformation -Encoding utf8

# CSV to TSV
Import-Csv -Path ./data.csv |
    Export-Csv -Path ./data.tsv -Delimiter "`t" -NoTypeInformation -Encoding utf8

The -UseCulture switch uses the current system's list separator instead of a comma. That can match local spreadsheet settings, but the same script then produces different files on machines with different regional settings. A fixed -Delimiter is more predictable for files that move between systems.

Files without a header row

If the first line is data rather than column names, supply the names with -Header. Otherwise the first record is consumed as the header:

Import-Csv -Path ./raw.txt -Header 'Date', 'Host', 'Status'

Will the file stay readable and usable after sharing?

A CSV file is plain text, so it copies, diffs, searches and edits cleanly in any text editor. There is no hidden formatting to lose. The practical risks sit in how the receiving application interprets the text.

  • Encoding mismatch: if the reader assumes a different encoding than the writer used, accented letters, non-Latin scripts and symbols can appear garbled. Some spreadsheet applications have been reported to misread UTF-8 files that lack a byte order mark. This was not tested here. If your recipients use a specific application, open a sample file containing non-ASCII characters in that application before you rely on it. In PowerShell 7, -Encoding utf8BOM writes a byte order mark when one is needed.
  • Delimiter expectations: an application configured for semicolon-separated values may put a comma-separated file into a single column. State the delimiter when you share the file, or match the recipient's settings.
  • Spreadsheet reinterpretation: a spreadsheet may convert values that look like numbers or dates, which can strip leading zeros from IDs or reformat dates. That happens in the receiving application, not in PowerShell, and its behavior varies. Check the specific application you use.
  • Accessibility: in a text editor or terminal, assistive technology reads the raw text, including delimiters and quotes, which is hard to follow for wide tables. Once the file opens in a spreadsheet, accessibility depends on that application. No assistive technology testing was supplied for this article, so treat any specific screen-reader claim as unverified.

When the audience is people rather than programs, a short plain-text summary or a formatted report is often easier to read than the raw file. Keep the CSV as the data source and share a readable version alongside it if needed.

Bottom line

PowerShell's CSV cmdlets are a dependable, built-in way to read, filter and convert flat tabular data, including between comma- and tab-delimited formats. Expect strings rather than types, columns taken from the first object, and flattened nested values. Set the delimiter and encoding explicitly, and include -NoTypeInformation for compatibility across versions. If your data is nested, needs its types preserved, or is meant only for people to read, choose JSON, CLIXML or a plain-text report instead.