Table of Contents
Using the Import-Csv
Cmdlet
To read a CSV file in PowerShell, use the Import-Csv
cmdlet.
1 2 3 4 |
$csvFile = "d:\data.csv" Import-Csv -Path $csvFile |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Country Age Salary Purchased ------- --- ------ --------- France 44 72000 No Spain 27 48000 Yes Germany 30 54000 No Spain 38 61000 No Germany 40 Yes France 35 58000 Yes Spain 52000 No France 48 79000 Yes Germany 50 83000 No France 37 67000 Yes |
PowerShell provides several cmdlets that allow us to work with CSV files:
Import-Csv
ConvertFrom-Csv
Export-Csv
ConvertTo-Csv
These cmdlets allow us to easily read, write, and manipulate CSV data in PowerShell, but Import-Csv
and ConvertFrom-Csv
are primarily used to read the specified CSV files.
The Import-Csv cmdlet in PowerShell reads the contents of a CSV file and converts it to an array object. It assumes that the first row of the CSV file contains the column headers and that a comma separates each column.
Its basic syntax is Import-Csv -Path
where -Path
is a mandatory parameter that specifies the path to the CSV file we want to import. For example, we used this cmdlet to read the csvFile
.
Using the ConvertFrom-Csv
Cmdlet
We can read a CSV file in PowerShell using the ConvertFrom-Csv
cmdlet.
Use the ConvertFrom-Csv
Cmdlet with the Pipe (|
) Operator
To read a CSV file:
- Use the
Get-Content
cmdlet with the-Raw
parameter to read the file as a single string. - Use the pipe (
|
) operator to pass the output of theGet-Content
cmdlet to theConvertFrom-Csv
cmdlet.
1 2 3 4 |
$csvFile = "d:\data.csv" (Get-Content -Path $csvFile -Raw) | ConvertFrom-Csv |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Country Age Salary Purchased ------- --- ------ --------- France 44 72000 No Spain 27 48000 Yes Germany 30 54000 No Spain 38 61000 No Germany 40 Yes France 35 58000 Yes Spain 52000 No France 48 79000 Yes Germany 50 83000 No France 37 67000 Yes |
Get-Content is a PowerShell cmdlet that reads content from a file or the specified item or items and returns the content in an object. For example, this cmdlet can read text from a file, such as a log file, and then perform operations on that text. By default, it reads the file as an array of strings, but its -Raw
parameter can read the file as a single string.
For example, we used the cmdlet with the -Raw
parameter to read the CSV file as a single string. Then to convert it into the CSV format, we used the ConvertFrom-Csv
cmdlet.
The ConvertFrom-Csv cmdlet is similar to Import-Csv
, but it converts the CSV data into an object in memory rather than reading it from a file. We passed the output of the Get-Content
operator as an input to the ConvertFrom-Csv
cmdlet using the pipe (|
) operator.
Use the ConvertFrom-Csv
Cmdlet with the -InputObject
Parameter
To read a CSV file, use the -InputObject
parameter of the ConvertFrom-Csv
cmdlet and specify the output of the Get-Content
cmdlet as the value.
1 2 3 4 |
$csvFile = "d:\data.csv" ConvertFrom-Csv -InputObject (Get-Content -Path $csvFile -Raw) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Country Age Salary Purchased ------- --- ------ --------- France 44 72000 No Spain 27 48000 Yes Germany 30 54000 No Spain 38 61000 No Germany 40 Yes France 35 58000 Yes Spain 52000 No France 48 79000 Yes Germany 50 83000 No France 37 67000 Yes |
The ConvertFrom-Csv
cmdlet in PowerShell converts the input string, in the CSV format, into an object. The basic syntax of the ConvertFrom-Csv
cmdlet is ConvertFrom-Csv -InputObject
.
The -InputObject
is a mandatory parameter that specifies the input string we want to convert into an object. This input string should be in the CSV format, with each column separated by a comma.
To create the input string, we used the Get-Content
cmdlet with the -Raw
parameter we discussed in the code section for using the pipe operator. Then we used this string as a value of the InputObject
parameter of the ConvertFrom-Csv
cmdlet and printed the result on the console.
Replace the Get-Content
Cmdlet
We can replace the Get-Content
cmdlet with other cmdlets and methods as follows:
- The
cat
cmdlet reads the file as a single string. - The [System.IO.File]::ReadAllText() method takes the file as an argument and returns it as a string.
Note that these commands read the file as a string. To read them as CSV, we can use the ConvertFrom-Csv
cmdlet we discussed in the above-mentioned code sections.