I have the following PowerShell code that imports a csv (that has 35 column) and modifies the "Account" column. It's working as expected. However, when I export, it's adding it as the first column when it should be the 7th position.
$Results = Import-Csv "$Env:FILEPATH\Metadata\Booking*.csv" | Select-Object @{Name = 'Account'; Expression = {@("A_"+$_."Account") -replace "[^0-9a-zA-z]",'_'}}, * -ExcludeProperty Account
# output to (new) csv file
$Results | Export-Csv -Path "$Env:FILEPATH\Metadata\Bookingtest.csv" -NoTypeInformation
Is there a way to preserve column order upon export? Thank you!
Instead of creating new objects with
Select-Object, update the property of the object you already have:From feedback in comments, to remove quotes from your Csv in PowerShell 5.1 before exporting to a file you can use the regex pattern shown in this answer, the code would be: