PowerShell: Import CSV and Extract Specific Columns


Often you may want to use the Import-Csv cmdlet in PowerShell to import a CSV file and only extract specific columns.

You can use the following syntax to do so:

Import-Csv $my_file | select team, assists

This particular example imports the CSV file specified at the path in the $my_file variable and specifies that only the team and assists columns should be extracted from the file.

The following example shows how to use this syntax in practice.

Example: Import CSV and Extract Specific Columns Using PowerShell

Suppose we have a CSV file saved in the following location:

  • c:\users\bobbi\nba_data.csv

We can use the Get-Content cmdlet to view the content of this file:

The file contains three columns that show the team name, points and assists for various basketball players.

Suppose that we would like to import this CSV file and only extract the team and assists columns.

We can use the following syntax to do so:

$my_file = 'c:\users\bobbi\nba_data.csv'
Import-Csv $my_file | select team, assists 

The following screenshot shows how to use this syntax in practice:

PowerShell import CSV and extract specific columns

Notice that we’re able to successfully import this CSV file and only extract the team and assists columns, just as we specified by using the select statement.

Note that if you would like to exclude specific columns, you could use the -ExcludeProperty operator.

For example, we can use the following syntax to import a CSV file and exclude the assists column:

$my_file = 'c:\users\bobbi\nba_data.csv'
Import-Csv $my_file | select * -ExcludeProperty assists 

The following screenshot shows how to use this syntax in practice:

PowerShell import CSV file and exclude specific columns

Notice that all columns from the CSV file are imported, excluding the assists column.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Export-Csv with No Headers
PowerShell: How to Use Import-Csv with No Headers
PowerShell: How to Use Import-Csv and Foreach

Leave a Reply