The Import-Csv command

Comma-Separated Value (CSV) files are plain text. Applications such as Microsoft Excel can work with CSV files without changing the file format, although the advanced features Excel has cannot be saved to a CSV file.

By default, Import-Csv expects input to have a header row, to be comma delimited, and to use ASCII file encoding. If any of these items are different, the command parameters may be used. For example, a tab may be set as the delimiter:

Import-Csv TabDelimitedFile.tsv -Delimiter `t 

A tick followed by t (`t) is used to represent the tab character in PowerShell.

Data that's imported using Import-Csv will always be formatted as a string. If Import-Csv is used to read a file containing the following text, each of the numbers will be treated as a string:

Name,Position 
Jim,35 
Matt,3 
Dave,5 

Attempting to use Sort-Object on the imported CSV file will result in values being sorted as if they were strings, not numbers:

PS> Import-Csv .positions.csv | Sort-Object Position

Name Position
---- --------
Matt 3
Jim 35
Dave 5

Sort-Object can be used to consider the value for Position as an integer by using a script block expression:

PS> Import-Csv .positions.csv | Sort-Object { [Int]$_.Position }

Name Position
---- --------
Matt 3
Dave 5
Jim 35

This conversion problem exists regardless of whether the data in a CSV file is a number, or a date, or any type other than string.

ConvertFrom-Csv is similar to Import-Csv, except that content is read from PowerShell instead of a file:

PS> "powershell,404" | ConvertFrom-Csv -Header Name, Id

Name Id
---- --
powershell 404
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.130.31