Change Order of CSV Columns

by Nov 5, 2012

If you have a CSV file and would like to change the order of columns, simply import it into PowerShell, use Select-Object to change the order, and then re-export the CSV file again!

$Path = "c:\somepathtocsv.csv"
(Import-CSV -Path $Path) | Select-Object -Property Column1, Column3, Column2 | Export-CSV -Path $Path

Note the parenthesis: they allow you to write back to the same file because PowerShell will first read in the CSV completely before it changes data and writes back to the same file.

Try this with some non-productive CSV material first. You may have to add the -Delimiter parameter to the CSV cmdlets if your CSV files are using a delimiter other than a comma.

Twitter This Tip! ReTweet this Tip!