Import Csv - PowerShell


Overview

Import-Csv lets you easily import data from a comma-separated value (CSV) file into PowerShell. It’s a convenient tool for working with structured data, especially when the data is stored in a tabular format.

Syntax

Import-Csv -Path <string> [-Delimiter <string>] [-Encoding <string>] [-Header <bool>] [-Skip <uint32>] [-Preview <bool>] [-UseCulture] [-ErrorAction <string>] [-WarningAction <string>] [-InformationAction <string>] [-Verbose] [-Debug] [-OutFile <string>] [-ReportAuthenticationStatus]

Options/Flags

  • -Path: Specifies the path to the CSV file. This parameter is mandatory.
  • -Delimiter: Defines the delimiter used in the CSV file. The default is a comma (,).
  • -Encoding: Sets the encoding used to read the CSV file. By default, UTF8 encoding is assumed.
  • -Header: Indicates whether the CSV file has a header row. Defaults to $false.
  • -Skip: Skips the specified number of rows at the beginning of the CSV file.
  • -Preview: Outputs only the first 10 rows of data for preview purposes.
  • -UseCulture: Specifies whether to process numbers and dates according to the culture settings of the current user.
  • -ErrorAction, -WarningAction, -InformationAction: Controls how errors, warnings, and informational messages are handled.
  • -Verbose: Outputs detailed information about the command’s execution.
  • -Debug: Outputs debugging information.
  • -OutFile: Exports the imported data to a new CSV file.
  • -ReportAuthenticationStatus: Displays the status of data retrieval operations if the underlying data source requires authentication.

Examples

Example 1: Import a CSV file with a header

Import-Csv -Path "user_data.csv" -Header

Example 2: Preview the first 10 rows of data

Import-Csv -Path "system_config.csv" -Preview

Example 3: Skip the first 5 rows and set the delimiter to semicolon

Import-Csv -Path "sales_data.csv" -Skip 5 -Delimiter ";"

Common Issues

  • CSV file not found: Ensure that the specified path points to a valid CSV file.
  • Incorrect delimiter: Verify that the -Delimiter parameter matches the delimiter used in the CSV file.
  • Data parsing errors: Check for invalid data formats or missing values in the CSV file.

Integration

  • Use Import-Csv in combination with Export-Csv to create structured CSV files from PowerShell data.
  • Import data from multiple CSV files using loops and variables.
  • Filter and sort imported data using PowerShell’s where and sort commands.
  • Export-Csv: Exports data to a CSV file.
  • ConvertFrom-Csv: Converts CSV data into PowerShell objects.
  • Format-Table: Formats objects in a tabular format similar to CSV.

Microsoft Docs: Import-Csv