PowerShell: How to Rename Column Headers in CSV File


You can use the following syntax in PowerShell to rename the column headers of a CSV file:

Import-Csv nba_data.csv |
Select-Object @{ expression={$_.team}; label='name' },
              @{ expression={$_.points}; label='pts' },
	      @{ expression={$_.assists}; label='ast' },
	      @{ expression={$_.rebounds}; label='reb' } |
Export-Csv -NoType nba_data_new.csv

This particular example renames the column headers in the CSV file named nba_data.csv and exports the results to a new CSV file named nba_data_new.csv.

Here is how the columns are renamed:

  • The team column is renamed to name.
  • The points column is renamed to pts.
  • The assists column is renamed to ast.
  • The rebounds column is renamed to reb.

Note that when we use the Export-Csv command to export the results to a new CSV file, we specify -NoType so that no #TYPE information is shown on the first line of the exported file.

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

Example: How to Rename Column Headers in CSV File Using PowerShell

Suppose we have a CSV file in our current directory named nba_data.csv that contains information about various basketball players.

We can use the Import-Csv cmdlet to view the contents of this file:

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

Suppose that we would like to rename each of the column headers of this file.

We can use the following syntax to do so:

Import-Csv nba_data.csv |
Select-Object @{ expression={$_.team}; label='name' },
              @{ expression={$_.points}; label='pts' },
	      @{ expression={$_.assists}; label='ast' },
	      @{ expression={$_.rebounds}; label='reb' } |
Export-Csv -NoType nba_data_new.csv

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

PowerShell rename column headers of CSV file

Notice that when we use the Import-Csv cmdlet to view the contents of the new file named nba_data_new.csv we can see that the file has the new column headers that we specified.

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