Import-Csv

Import comma-separated value (CSV) files in the format produced by Export-CSV.

Syntax
      Import-Csv [[-Delimiter] char] [-path] string[]
         [-Header string[]] [CommonParameters]

      Import-CSV -UseCulture [-Path] string[]
         [-Header string[]] [CommonParameters]

Key
   -Delimiter <char>
       The delimiter that separates the property values in the CSV file.
       The default is a comma (,).
       Enter a character, such as a colon (:). To specify a semicolon (;), enclose it in quotation marks.

       If a character is specified which is not the string delimiter in the file,
       Import-CSV cannot create objects from the CSV strings. Instead, it returns the strings.

    -Header string[]
       An alternate column header row for the imported file.
       The column header determines the names of the properties of the object created.

       Enter a comma-separated list of the column headers.
       Enclose each item in quotation marks (single or double).
       Do not enclose the header string in quotation marks. If fewer column headers are
       entered than there are columns, the remaining columns will have no header.
       If more headers are entered than there are columns, the extra headers are ignored.

       When using -Header, delete the original header row from the CSV file. Otherwise,
       Import-CSV will create an extra object from the items in the header row.

   -Path path
       The path to the CSV file {may be piped}

   -UseCulture
       Use the list separator for the current culture as the item delimiter.
       The default is a comma (,).

       To find the list separator for a culture, use the following command:
         (Get-Culture).TextInfo.ListSeparator. 
       If a character is specified which is not the delimiter used in the CSV strings,
       ConvertFrom-CSV cannot create objects from the CSV strings. Instead, it returns the strings.

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

Standard Aliases for Import-CSV: ipcsv

The ConvertTo-CSV and ConvertFrom-CSV cmdlets can also be used to convert objects to CSV strings (and back). These cmdlets are the same as the Export-Csv and Import-CSV cmdlets, except that they do not save the CSV strings in a file.

All items imported from a text CSV file will be imported as strings, so you may want to cast those values into the correct Data Type.

Examples

Import and display the comma separated file prices.txt

 Product,Price
 Broccoli,75
 Carrots,122
 Beans,350

$PriceList = Import-Csv prices.txt 

ForEach ($item in $Pricelist){ 
  $Description = $($item.Product)
  $Price = $($item.Price)
  Write-host $Description
  Write-host $Price
}

Export and then import a CSV file of Microsoft .NET Framework objects:

PS C:\> get-process | export-csv processes.csv
PS C:\> $p = import-CSV processes.csv
PS C:\> $p | out-gridview

Export processes to a file that uses a colon (:) as a delimiter:

PS C:\> get-process | export-csv processes.csv -Delimiter :
PS C:\> $p = import-csv processes.csv -Delimiter :

“A thing that has no value does not exist” ~ Robert Pirsig

Related PowerShell Cmdlets:

Export-Csv - Export to Comma Separated Values (spreadsheet).


 
Copyright © SS64.com 1999-2019
Some rights reserved