PowerShell: How to Use Export-Csv and Specify Column Order


Often you may want to use the Export-Csv cmdlet in PowerShell to export data to a CSV file with a specific column order.

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

$data | Select-Object 'rebounds', 'assists', 'points' | Export-Csv 'new_data.csv' -NoTypeInformation

This particular example will export the data saved in the variable named $data to a CSV file named new_data.csv and the columns will be in the following order by name: rebounds, assists, points.

By using the Select-Object cmdlet before using the Export-Csv cmdlet, we are able to specify the exact column order to use.

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

Example: How to Use Export-Csv and Specify Column Order in PowerShell

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

  • c:\users\bobbi\basketball_data.csv

Here is what this file looks like when we open it in Notepad:

Suppose that we would like to import this file into PowerShell, then export it and use the following column order: rebounds, assists, points.

We can use the following syntax to do so:

$data = Import-Csv -Path c:\users\bobbi\basketball_data.csv
$data | Select-Object 'rebounds', 'assists', 'points' | Export-Csv 'new_data.csv' -NoTypeInformation

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

PowerShell export-csv column order

We can then navigate to this CSV file that we exported named new_data.csv and view it:

Notice that the data has been successfully exported to a CSV file and the columns are in the exact order that we specified: rebounds, assists, points.

Note: We included -NoTypeInformation to remove all of the extra file 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 List All Files in Directory to Text File
PowerShell: How to Delete All Files with Specific Extension

Leave a Reply