Select columns when importing CSV in PowerShell

How to keep only the CSV columns you need in PowerShell with Import-Csv and Select-Object, what the pipeline changes, and what to check before exporting or sharing.

Import-Csv has no parameter for choosing columns. It reads every column in the file and turns each row into an object with one property per header. To keep only some columns, you pipe those objects to Select-Object and name the properties you want. This review covers what that pipeline changes, when it fits, and what to check before you save or share the result.

Is this approach suitable for your use?

For most column-selection jobs, Import-Csv piped to Select-Object is the right tool. Its main benefit is that it parses CSV properly. Quoted fields that contain commas or line breaks stay intact, which naive text splitting cannot guarantee. The main tradeoff is that every value comes back as a string. Header quirks such as duplicate names also need attention before selection works cleanly.

Plain text handling is preferable in a few cases: when the file is not really CSV, such as a fixed-width or log format; when you must preserve the original bytes exactly, including quoting style and line endings; or when you only need to inspect a few lines with Get-Content. The evidence supplied for this article does not include performance measurements, so this article makes no claims about speed or memory on large files.

The basic pattern

Assume a file named customers.csv with this content:

Id,Name,City,Status,Notes
1,Ana Ruiz,Denver,Active,"Prefers mail, not calls"
2,Lee Park,Austin,Paused,

To keep only Id, Name and Status:

Import-Csv .\customers.csv | Select-Object Id, Name, Status

To write that subset to a new file:

Import-Csv .\customers.csv |
    Select-Object Id, Name, Status |
    Export-Csv .\subset.csv -NoTypeInformation

With default settings in both Windows PowerShell 5.1 and PowerShell 7, Export-Csv quotes every field, so subset.csv should look like this:

"Id","Name","Status"
"1","Ana Ruiz","Active"
"2","Lee Park","Paused"

The Notes value in row 1 contains a comma inside quotes. Import-Csv reads it as a single field, so dropping or keeping that column does not shift the other values. A split on commas with Get-Content and -split would break that row into the wrong number of fields.

What the transformation actually changes

Nothing happens to the source file. Import-Csv reads it and creates objects in memory, and Select-Object creates new objects that carry only the named properties. The data changes only when you write it out, usually with Export-Csv. Until then the result exists only in the pipeline or in a variable.

Three things differ from the original once you export:

  • Column order follows the order you list in Select-Object, not the order in the source file.
  • Quoting is normalized by Export-Csv, so the output may quote fields the source left unquoted.
  • Encoding and line endings follow Export-Csv defaults, which differ between Windows PowerShell 5.1 and PowerShell 7. Pass -Encoding explicitly if the destination expects a specific encoding.

Formatting is a separate concern from data. Format-Table and Format-List only control how objects appear in the console. Their output is formatting instructions, not data, so piping it to Export-Csv produces meaningless output. Use Select-Object to choose the data and Format-Table only to view it.

Variations that come up often

Need | Approach
Column name contains spaces | Quote it: Select-Object 'First Name', Status
Keep everything except a few columns | Select-Object -Property * -ExcludeProperty Notes, City (works in 5.1 and 7)
Rename a column | Select-Object @{Name='CustomerId'; Expression={$.Id}}, Name
Convert a value to a number | Select-Object @{Name='Id'; Expression={[int]$.Id}}, Name
Match columns by pattern | Select-Object -Property Name, Addr* (wildcards are supported)
File uses semicolons or tabs | Import-Csv .\file.csv -Delimiter ';' (use "`t" for tabs)
CSV text is already in a variable | $text | ConvertFrom-Csv | Select-Object Id, Name

The -Header parameter of Import-Csv replaces column names. It does not select columns. If the file already has a header row, -Header turns that row into an ordinary data row. This is useful for headerless files or for renaming broken headers. You still need Select-Object afterward to drop columns.

Pitfalls that change meaning silently

  • Misspelled column names do not raise an error. Select-Object adds the property with an empty value, so a typo yields a blank column rather than a failure. Compare against the headers first, for example with (Import-Csv .\customers.csv | Select-Object -First 1).PSObject.Properties.Name.
  • Header whitespace counts. A header saved as 'Status ' with a trailing space will not match Status.
  • Duplicate header names cause Import-Csv to fail. Supply clean names with -Header, then skip the original header row.
  • Everything is a string. Sorting or comparing numbers and dates without converting them gives text-order results, such as 10 sorting before 9.
  • Windows PowerShell 5.1 adds a #TYPE line to Export-Csv output unless you pass -NoTypeInformation. PowerShell 7 omits it by default, and the switch is harmless there.

Will the result stay usable after copying or sharing?

The exported file is ordinary delimited text, so the plain text itself is the shareable form. No styling or special characters are introduced, and editing, searching and screen-reader access depend on the application that opens the file. That is a property of plain CSV in general, not a tested result. No accessibility testing with specific assistive technologies was available for this article.

The more practical risk is how the receiving program interprets values. Spreadsheet applications may reformat fields on open, for example by dropping leading zeros from IDs or converting date-like strings. That depends on the program and its import settings, and no destination-specific test results were supplied here. If the recipient will open the file in a particular tool, run one export through that tool and check a few sensitive columns before relying on it.

Copying console output is a different matter. Text copied from a Format-Table display is padded and may be truncated to fit the window. It is not valid CSV. Share the exported file, or use ConvertTo-Csv when you need CSV text on the clipboard or in a message.

Gaps in the available evidence

This article draws on the documented behavior of the cmdlets. It does not include benchmarks for large files, compatibility tests with named spreadsheet or database tools, or assistive-technology testing. If any of those matter for your workflow, a small scoped test with your actual file and destination is the reliable way to confirm the outcome.