Change Order of CSV Columns

by | November 5,2012

Table of Contents

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

Why Parentheses Matter

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.

-Delimiter parameter

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.

ReTweet this Tip!