Read CSV File in PowerShell

Read CSV file in PowerShell

Using the Import-Csv Cmdlet

To read a CSV file in PowerShell, use the Import-Csv cmdlet.

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 the Get-Content cmdlet to the ConvertFrom-Csv cmdlet.

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.

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:

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *