Import CSV files in PowerShell

How PowerShell's Import-Csv turns CSV and TSV text into objects, what it changes, where meaning can be lost, and when plain text or another approach fits better.

PowerShell's Import-Csv cmdlet reads a delimited text file and turns each row into an object you can filter, sort and export. That makes it a common way to work with CSV and TSV data from the command line. Whether it suits a given job depends on whether the conversion keeps the data's meaning intact and whether the output stays usable where it ends up. This review covers what Import-Csv actually changes, where it can mislead, and when keeping the file as plain text is the better choice.

Is Import-Csv suitable for your task?

Import-Csv is a good fit when you need to query or reshape tabular data inside a script, for example selecting rows, comparing columns, or feeding values into other commands. Its main benefit is that columns become named properties, so you refer to a value by its header name instead of splitting strings by hand. Quoted fields that contain the delimiter are parsed as single values.

The main tradeoff is that the result is a set of objects in memory, not the original text. Every value comes in as a string, the original formatting is discarded, and writing the data back out with Export-Csv produces a new file that may differ from the source in quoting, encoding and header lines. If you only need to read, search, diff or forward the file unchanged, plain text tools such as Get-Content or Select-String are simpler and leave the bytes untouched.

What the conversion actually changes

Import-Csv does not change the file on disk. It reads the file and produces one custom object per data row. By default, the first line supplies the property names and each later line supplies the values. What you see printed in the console is a presentation of those objects through PowerShell's formatting system. Commands like Format-Table change only how the objects are displayed. They do not change the stored values.

Consider a small file named users.csv with this content:

Name,Department,EmployeeId
Ana,Sales,00172
"Lee, Sam",Support,00419

Importing it and filtering by a column looks like this:

$users = Import-Csv -Path .\users.csv
$users | Where-Object { $_.Department -eq 'Support' }

The import yields two objects with the properties Name, Department and EmployeeId. Because the second name is quoted, "Lee, Sam" stays one value and is not split at the comma. The employee IDs keep their leading zeros because they are stored as the strings 00172 and 00419, not as numbers. The filter returns only the Support row.

Values are strings until you convert them

Because every field is text, comparisons and sorting are text-based unless you cast the value. Sorting a numeric column as text puts 10 before 9, for example. Cast explicitly when numeric or date meaning matters:

$scores = Import-Csv -Path .\scores.csv
$scores | Sort-Object { [int]$_.Score } -Descending

The string behavior also has a benefit. Identifiers such as ZIP codes, account numbers or part numbers keep their exact characters, including leading zeros, unless your own code converts them.

Parameters that change how text is interpreted

Parameter | Effect
-Path / -LiteralPath | Selects the file. -LiteralPath treats wildcard characters such as brackets as literal characters in the file name.
-Delimiter | Sets the field separator. Use "`t" for tab-separated (TSV) files or ';' for semicolon-separated files.
-UseCulture | Uses the list separator from the current culture settings instead of a comma.
-Header | Supplies property names for files with no header row. If the file already has a header, that row is imported as a data row.
-Encoding | Sets the character encoding used to read the file.

For example, a tab-separated file is read with:

Import-Csv -Path .\export.tsv -Delimiter "`t"

Use double quotes around `t so PowerShell expands it to a tab character. In single quotes it stays the literal two characters.

Header problems

Header names become property names, so the header row needs attention. Spaces or punctuation in a header mean you must quote the property name, as in $_.'First Name'. Duplicate column names cause the import to fail, because an object cannot have two properties with the same name. When a header cell is empty, PowerShell's documentation says it substitutes a default name and shows a warning. Check the resulting property names before relying on them in a script.

Importing CSV text that is not in a file

If the CSV content is already in a string or comes from another command's output, ConvertFrom-Csv performs the same conversion without reading a file. It accepts the same delimiter and header options.

Will the data stay readable and usable after export or sharing?

Most problems show up at the edges: reading the file correctly in the first place and writing it back in a form other tools accept.

Encoding

Default encodings differ between Windows PowerShell 5.1 and PowerShell 7 and later. A file with accented letters, non-Latin scripts or other non-ASCII characters can import with garbled characters if PowerShell guesses the encoding wrong. When the data contains anything beyond basic ASCII, pass -Encoding explicitly on both Import-Csv and Export-Csv instead of relying on the default. The receiving program may also expect a particular encoding, with or without a byte order mark. Check that program's documentation rather than assuming.

Round trips with Export-Csv

Exporting imported objects creates a new file, not a copy of the original. Watch for these differences:

  • In Windows PowerShell 5.1, Export-Csv adds a #TYPE information line at the top unless you use -NoTypeInformation. PowerShell 6 and later omit it by default.
  • Export-Csv quotes field values by default. PowerShell 7 adds -UseQuotes and -QuoteFields to control this.
  • Values you cast to numbers or dates are written back in PowerShell's string form. That form may not match the original formatting.
  • The delimiter and encoding of the output follow the parameters you pass to Export-Csv, not the original file's settings.

If a downstream system is strict about format, compare the exported file against a known-good sample before relying on it.

Console output is not a data format

Text copied from a Format-Table display is aligned with spaces, may truncate long values, and loses the original delimiters and quoting. It is fine for reading on screen but not reliable for pasting into another tool or sharing as data. To share results, export them with Export-Csv or ConvertTo-Csv so recipients get properly delimited text. Delimited text is also a better input than a screen capture of console output for people using screen readers or other assistive technology.

Large files

Import-Csv sends objects down the pipeline as it reads rows. Assigning the whole result to a variable, however, holds every row in memory at once. For very large files, processing rows in a pipeline instead of storing them first reduces memory pressure. Actual limits depend on the machine and the data, and no measurements were available for this review.

When plain text is the better choice

  • You need the file byte-for-byte unchanged, for example for checksums, audits or forwarding.
  • You are searching for a string or pattern, which Select-String handles without parsing.
  • The file is not consistently delimited, such as a log with free-form lines, so property mapping would be unreliable.
  • The header row has duplicate or unusable names and you cannot supply replacements with -Header.

Gaps in this review

No version-specific test results, performance figures or destination compatibility tests were supplied. The behavior described here comes from PowerShell's documented cmdlet behavior. Parameter availability and defaults differ between Windows PowerShell 5.1 and PowerShell 7, so confirm details with Get-Help Import-Csv -Full on the version you actually run. Test exported files in the specific application that will receive them.

Bottom line

Import-Csv is a sound choice for querying and transforming well-formed CSV or TSV data in PowerShell. Its object model preserves exact text values, and the main risks are untyped strings, encoding mismatches and changes introduced on export. Set the delimiter and encoding explicitly, cast values where numeric meaning matters, and keep the original file as plain text when it must stay unchanged.