Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

csv

I'm new to powershell, so the script is a Frankenstein of various examples of from sites.

My question is how can I make sure that a csv file I am creating for a DataTable keeps the column order I specify?

My script does this to populate the csv headers and values like so:

...snip...
$dataTable | ForEach-Object {
            $csv+=New-Object PSObject -Property @{
                program_code=$_.ProgramCode;
                first_name=$_.FirstName;
                last_name=$_.LastName;
                email=$_.email;
                phone=$_.phone;
                phone2=$_.otherphone;
                address=$_.addr1;
                address2=$_.addr2;
                city=$_.city;
                state=$_.state;
                postal_code=$_.Zip;
                country=$_.Country;
                grad_year=$_.HsGradDate;
                lead_date=$_.LeadDate;
                lead_source=$_.LeadSource;
                school_status=$_.SchoolStatus;
        }
        }
    $csv | Export-CSV C:\scripts\NewLeads$(Get-Date -Format yyyyMMdd).csv -notype -Append
...snip...

I want the file to have to columns in the order that I specify in the script, but when I open it in notepad or excel the columns appear in a seemingly random order. Keyword being seemingly since they may have some method of ordering themselves.

like image 331
ledgeJumper Avatar asked Sep 25 '13 20:09

ledgeJumper


People also ask

Does PowerShell execute sequentially?

The Sequence keyword runs selected workflow activities sequentially. Workflow activities run in the order that they appear and do not run concurrently. The Sequence keyword is only valid in a PowerShell Workflow. The Sequence keyword is used in a Parallel script block to run selected commands sequentially.

How do I sort in ascending order in PowerShell?

The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties aren't included in a command, PowerShell uses default sort properties of the first input object.

What does CLC do in PowerShell?

Description. The Clear-Content cmdlet deletes the contents of an item, such as deleting the text from a file, but it does not delete the item. As a result, the item exists, but it is empty. Clear-Content is similar to Clear-Item , but it works on items with contents, instead of items with values.

How do I sort PowerShell output?

To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.


1 Answers

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 |
    ...
like image 105
Jason Shirk Avatar answered Oct 07 '22 10:10

Jason Shirk