Import-Csv

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

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

      Import-Csv [[-Delimiter] char] [-LiteralPath] string[]
         [-Header string[]] [-Encoding CharSet] [CommonParameters]

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


      Import-CSV -UseCulture [-LiteralPath] string[]
         [-Header string[]] [-Encoding CharSet] [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}

   -LiteralPath path
       The path to the CSV file to import.
       Unlike -Path, the value of the -LiteralPath parameter is used exactly as it is typed.
       No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single
       quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.

   -Encoding CharSet
       Encode in a specific character set:
           ASCII             ASCII (7-bit) character set.
           BigEndianUnicode  UTF-16 format big-endian byte order.
           oem               Use the default encoding for MS-DOS and console programs.
           UTF8              UTF-8 format.
           utf8BOM           UTF-8 format with Byte Order Mark (BOM)
           utf8NoBOM         UTF-8 format without Byte Order Mark (BOM)
           utf32             UTF-32 format.
           UTF7              UTF-7 format.
           Unicode           UTF-16 format little-endian byte order.

           The default encoding is utf8NoBOM.
           Beginning with PowerShell 6.2, the Encoding parameter also allows numeric IDs of registered
           code pages (like -Encoding 1251) or string names of registered code pages (like -Encoding "windows-1251").
           For more information, see the .NET documentation for Encoding.CodePage.

   -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.

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 © 1999-2022 SS64.com
Some rights reserved