The Import-CSV cmdlet allows you to import the data from a CSV file to any other file or to the PowerShell console. The purpose of this article is to explain the working and usage of the Import-CSV cmdlet in PowerShell. The learning outcomes of this post are listed below:
- How Does the Import-CSV cmdlet Work?
- How to Use the Import-CSV cmdlet?
How Does the Import-CSV Cmdlet Work?
Basically, Import-CSV retrieves the information that is presented in separate files and generates a reference list or table. After importing the data, the foreach cmdlet is used to perform functions by iteratively extracting the content of the data in a horizontal manner. The syntax which is used to import CSV files into PowerShell is as follows:
Import-CSV <-Path> <-Delimiter> <-Encoding> <-Header> <-LiteralPath> <CommonParameters>
In PowerShell, each parameter has its own importance, which is written in the above syntax and is explained below with a short description.
- Import-CSV: Creates an object having the property of a table that extracts the items present in the CSV file.
- Path: Provides the location of the CSV file located in the system.
- Delimiter: Specify the character to separate the values in the file, such as semicolons (;).
- Encoding: Provides the encoding features of CSV files such as ASCII, UTF8, etc.
- Header: Represents the custom header, which is also known as the property name.
- LiteralPath: This path parameter works same as the path parameter. However, it excludes the wild card characters (if any).
- CommonParameters: These parameters can be used with all the cmdlets in PowerShell such as Debug ErrorAction, Verbose, WarningAction and so on.
How to Use the Import-CSV Cmdlet?
The Import-CSV is a powerful utility to manage the data in a CSV file. In this section, we have presented a set of examples to demonstrate the usage of the Import-CSV cmdlet in PowerShell.
Note: As a reference, we are using a test.csv file from which we will show how data is handled using the Import-CSV cmdlet. The test.csv file contains the following data:
Example 1: Import All the Data Using Import-CSV
In this example, we are importing all the data stored inside an excel spreadsheet. If you want to import a CSV file, you need to specify its absolute path in the Import-CSV cmdlet. The ft keyword is used as an alternative method to store data in a table. In our case, the following command will help retrieve data from a test.csv file:
Import-CSV -Path C:UsersDELLDesktoptest.csv | ft
The output shows that the data extracted from a file is printed on the PowerShell terminal in a tabular form using the ft keyword.
Example 2: Extract the Specific Data Using Import-CSV
With the help of the Import-CSV cmdlet, you can print or import specific data. For instance, if there are multiple columns in an excel sheet and you want to get only one of them. You can do it by utilizing the Import-CSV cmdlet.
As an example, the following Import-CSV cmdlet retrieves the data from the file and stores it in a variable. After that, only the Age column from the retrieved data will be printed:
$users = Import-Csv -Path C:UsersDELLDesktoptest.csv
The output shows that only the $Age data is printed on the console.
Example 3: Extract Multiple Columns From a CSV File
In this method, we have extracted multiple properties of data using the Import-CSV cmdlet in PowerShell. An example command is provided below.
Write-Host «IMPORTING A CSV FILE IN POWERSHELL»
$file= Import-Csv -Path «C:UsersDELLDesktoptest.csv»
Write-Host «THE FILE IS IMPORTED USING CMDLET»
write-host «THE LIST OF SALARY IN THE CSV FILE»
write-host «THE LIST OF AGE IN THE CSV FILE»
In the code, first specify the location of the file that is being imported into PowerShell. After that, extract the properties of Salary e Age using the object $file.
The outcome of the code shows that two properties, Salary, and Age, are extracted from the file and printed on the console.
The Import-CSV cmdlet retrieves the data from a CSV file. You can either go for the whole data retrieval, or you can also get the specific data. This post demonstrates the working and usage of the Import-CSV cmdlet in PowerShell. In this post, the working and usage of the Import-CSV cmdlet are explained in detail.