Is there a way to force powershell -Export-CSV cmdlet to maintain a specific column order? Is there a way to force powershell -Export-CSV cmdlet to maintain a specific column order? powershell powershell

Is there a way to force powershell -Export-CSV cmdlet to maintain a specific column order?


In PowerShell V3, instead of:

        $csv+=New-Object PSObject -Property @{

I would use:

        $csv+=[pscustomobject]@{

The PowerShell V3 parser will preserve the order of the keys when you cast a hash literal to either [ordered] or [pscustomobject]. A small side benefit to this approach - it will also be faster.

If you are using V2, you'll need to skip the -Property parameter to New-Object and instead use multiple calls to Add-Member. It will look something like:

$csv+=New-Object PSObject |    Add-Member -Name program_code -Value $_.ProgramCode -MemberType NoteProperty -PassThru |    Add-Member -Name first_name -Value $_.FirstName -MemberType NoteProperty -PassThru |    ...


Select the fields in the order required, then export.

$csv | select-object -property program_code,first_name,last_name,email,phone,phone2,address,address2,city,state,psotal_code,country,grad_year,lead_date,lead_source,school_status |Export-CSV C:\scripts\NewLeads$(Get-Date -Format yyyyMMdd).csv -notype -Append

However, you may be able to short-circuit this a little. Depending on what $dataTable really is, you may (should, in most cases) be able to select directly from that object and bypass creating the collection of PSObjects. But if you need the custom headers, you'll need to use expressions in select-object (linebreaks for readability).

$dataTable| select-object @{Name="program_code";Expression={$_.ProgramCode}},`@{Name="first_name";Expression={$_.firstname}},`@{Name="last_name";Expression={$_.lastname}},email,phone,`@{Name="phone2";Expression={$_.otherphone}},`@{Name="addr1";Expression={$_.address}},`@{Name="addr2";Expression={$_.address2}},city,state,`@{Name="postal_code";Expression={$_.zip}},country,`@{Name="grad_year";Expression={$_.hsgraddate}},`@{Name="lead_date";Expression={$_.leaddate}},`@{Name="lead_source";Expression={$_.leadsource}},`@{Name="school_status ";Expression={$_.schoolstatus }}| Export-CSV C:\scripts\NewLeads$(Get-Date -Format yyyyMMdd).csv -notype -Append