PowerShell Import-Csv
last modified February 15, 2025
In this article, we will cover the Import-Csv
cmdlet in
PowerShell. This cmdlet imports data from CSV files and creates objects
from the data.
CSV basics
CSV (Comma-Separated Values) is a simple file format for storing tabular data. Each line represents a record, and fields are separated by commas. PowerShell can read and manipulate CSV files efficiently. The first row typically contains column headers.
Basic Import-Csv usage
The simplest way to use Import-Csv
is with just the file path.
This reads the CSV file and creates objects with properties from the headers.
Each row becomes a separate object with corresponding property values.
Import-Csv -Path "data.csv"
This command reads data from data.csv and outputs objects. The first row should contain column names. Each subsequent row creates an object with those properties.
Working with CSV data
After importing CSV data, you can work with it like any PowerShell object. Properties correspond to column headers. You can filter, sort, and process the data using standard PowerShell cmdlets.
$data = Import-Csv "employees.csv" $data | Where-Object { $_.Department -eq "IT" }
This imports employee data and filters for IT department employees. The Where-Object cmdlet filters based on the Department property. The $_ variable represents each object in the pipeline.
Specifying delimiter
CSV files sometimes use different delimiters like semicolons or tabs. Use the -Delimiter parameter to specify non-comma separators. This ensures proper parsing of fields in the input file.
Import-Csv -Path "data.txt" -Delimiter ";"
This command reads a semicolon-delimited file. The delimiter must match what's used in the file. PowerShell will split each line at semicolons instead of commas.
Selecting specific columns
You can select specific columns from the CSV file using Select-Object. This is useful when you only need certain data. It reduces memory usage and simplifies output.
Import-Csv "products.csv" | Select-Object Name, Price
This imports product data but only keeps the Name and Price columns. The output objects will have only these two properties. Other columns are discarded from the output.
Converting data types
CSV data is imported as strings by default. You can convert properties to other types using calculated properties. This enables numerical operations on imported data.
Import-Csv "sales.csv" | Select-Object *, @{Name="Amount"; Expression={[int]$_.Amount}}
This converts the Amount column to integers. The @{} syntax creates a calculated property. Original columns are preserved with Select-Object *.
Handling headers
When a CSV file lacks headers, you can specify them with the -Header parameter. Provide an array of column names. These become property names for the imported objects.
Import-Csv "data_noheaders.csv" -Header "ID", "Name", "Value"
This imports a headerless CSV file and assigns column names. The first row becomes data, not headers. All objects will have ID, Name, and Value properties.
Processing large CSV files
For large CSV files, use the pipeline efficiently to minimize memory usage. Process rows one at a time rather than loading all data at once. This approach scales better with large datasets.
Import-Csv "largefile.csv" | ForEach-Object { # Process each row here $_.Name }
This processes each row individually as it's read from the file. The ForEach-Object cmdlet handles one row at a time. Memory usage remains constant regardless of file size.
Source
In this article, we have covered the Import-Csv cmdlet in PowerShell.
Author
List all PowerShell tutorials.