PowerShell: How to Remove Column from CSV File


Often you may want to use PowerShell to remove a column from an existing CSV file.

Fortunately this is easy to do by using the following syntax:

Import-Csv $my_file |
Select-Object * -ExcludeProperty points |
Export-Csv new_data.csv -NoTypeInformation

This particular example will remove the column named points from the existing CSV file located at the path specified by the $my_file variable and save the results to a new CSV file named nba_data.csv.

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

Related: How to Add Column to CSV File Using PowerShell

Example: How to Remove Column from CSV File 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 contents of this file:

We can see that the file currently has three columns: team, points, and assists.

Suppose that we would like to remove the column named points from the file.

We can use the following syntax to do so:

$my_file = "c:\users\bobbi\nba_data.csv"

Import-Csv $my_file |
Select-Object * -ExcludeProperty points |
Export-Csv nba_data2.csv -NoTypeInformation

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

PowerShell remove column from CSV file

We can see that the points column has been removed from the file.

Also note that we could list multiple column names separated by commas following -ExcludeProperty to remove multiple columns.

For example, we can use the following syntax to remove both the points and assists columns from the CSV file:

$my_file = "c:\users\bobbi\nba_data.csv"

Import-Csv $my_file |
Select-Object * -ExcludeProperty points, assists |
Export-Csv nba_data2.csv -NoTypeInformation

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

PowerShell remove multiple columns from CSV file

We can see that both the points and assists columns have been removed.

Note: When using Export-Csv we included -NoTypeInformation to remove the #TYPE information that would otherwise appear in the header of the CSV file.

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 Export-Csv with No Quotes
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply