PowerShell Genie

Import-Csv

Use Import-Csv to read a CSV file and return one object per row, with the header line becoming property names.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Import-Csv

Syntax

1PS> Import-Csv [[-Path] <string[]>] [-Delimiter <Char>] [-Header <String[]>] [-Encoding <Encoding>] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Import-Csv [[-Path] <string[]>] [-Delimiter <Char>] [-Header <String[]>] [-Encoding <Encoding>] [<CommonParameters>]
  1. Stage 1Import-Csv-Path -Delimiter -Header -Encoding

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Select first 3

    1PS> Import-Csv 'C:\Temp\users.csv' | Select-Object -First 3 Name, Department
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name         Department
    ----         ----------
    Jane Smith   Sales
    Alex Jones   IT
    Mina Patel   Finance

    Import-Csv -Delimiter ';'

    1PS> Import-Csv 'C:\Temp\users.csv' -Delimiter ';' | Select-Object -First 2
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name   Status   Owner
    ----   ------   -----
    SRV01  Healthy  Platform
    SRV02  Healthy  Platform

    Filter by Age

    1PS> Import-Csv 'C:\Temp\users.csv' | Where-Object { [int] $_.Age -gt 30 } | Select-Object Name, Age
    Show reference output

    Sample output · fictional data, shaped like the real thing

    Name         Age
    ----         ---
    Jane Smith   34
    Mina Patel   41

    Parameters

    Parameter definitions:

    • -Path
    • -Delimiter
    • -Header
    • -Encoding

    Import-Csv on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Import-Csv when you need to read a CSV file and return one object per row, with the header line becoming property names. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: One unexpectedly wide property may mean the delimiter is wrong. Inspect the first line and select the actual delimiter, then verify that the parsed property names match the expected headers. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Export-Csv. Import-Csv turns rows into objects using column headers; Export-Csv writes object properties as columns. Formatting a table before export discards the original reporting objects. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Import-Csv reference on Microsoft Learn

    Recap

    Import-Csv Use Import-Csv to read a CSV file and return one object per row, with the header line becoming property names.

    Next: Format-List
    Genie guideImport-Csv

    Use Import-Csv to read a CSV file and return one object per row, with the header line becoming property names. The console below is pre-loaded — run it, then change a value and run it again.