Powershell

Managing CSV Files in PowerShell With Import-CSV

A CSV(Comma Separated Value) file contains values separated by commas. It is saved with the “.csv” extension. The “CSV” files can be imported in PowerShell using the “Import-CSV” cmdlet. This cmdlet helps the “CSV” file be formatted as a table. Moreover, it can select and display a single entry in the “CSV” file in the PowerShell console.

This blog will discuss the method to manage the CSV files using the “Import-CSV” cmdlet.

How to Manage CSV Files in PowerShell Using “Import-CSV” Cmdlet?

The cmdlet “Import-csv” imports the CSV file in PowerShell in a table-like format. For further understanding of managing the CSV files overview the given examples.

Example 1: Import or Display Content of CSV File in PowerShell Console Using the “import-csv” Cmdlet

This demonstration will import the CSV file in PowerShell console using the “import-CSV” cmdlet:

import-csv C:\Doc\Employees.csv

According to the above code:

  • First, write the “Import-CSV” cmdlet and add the CSV file path:

Example 2: Import Only a Selected Query of the CSV File Using the “import-csv” Cmdlet

This illustration will import the selected query selected by the “Select-Object” cmdlet:

import-csv C:\Doc\Employees.csv | Select-Object Profession

In the above-stated code:

  • First, add the “Import-csv” cmdlet, then specify the CSV file path.
  • After that, define the pipeline “|” to transfer output from one command to another.
  • Then, add the “Select-Object” cmdlet and assign the “Profession” section to be imported and displayed in the output console of PowerShell:

Example 3: Select and Display Only a Single Value or Row With the Aid of the “Where” Cmdlet

This example will display only the selected values in the PowerShell console:

import-csv C:\Doc\Employees.csv | where{$_.Age -eq "28"}

According to the above code:

  • Add the “where{}” cmdlet after the pipeline “|”.
  • Inside the “where{}” cmdlet parameters, select the “Age” query followed by the “-eq” operator and then assign the “28” value to it:

Example 4: Add the Header Section With the Aid of the “-Header” Parameter

In this example, the header section will be created if it is not provided. For that reason, add the “-Header” parameter after the file path, and then define the header values within inverted quotes separated by a comma:

import-csv C:\Doc\Employees.csv -Header "Name", "Profession", "Age", "City"

Example 5: Display the First Two Rows of the CSV File Using “Import-Csv” Cmdlet

This example will display only the first two lines of the CSV file. For that reason, add the “-First” parameter after the “Select-Object” cmdlet and assign the value “2” to it:

import-csv C:\Doc\Employees.csv | Select-Object -First 2

That’s how managing CSV files in PowerShell.

Conclusion

The CSV files can be managed using the “Import-CSV” cmdlet. This command imports the CSV files in the PowerShell console in a table-like format. It uses parameters to select or display the desired queries or values. This article has illustrated the management of CSV files using several examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.