none
when i use Export-Csv the columns are not ordered in the correct order

    Question

  • See the below.After looking at the csv file the columns are not in order

    $Results = @()
    foreach ($item in $list.items) {
    $obj = New-Object PSObject -Property @{
      Title = $item["Title"]
      Description = $item["Description"]
      FromDate = $item["From Date"]
      ToDate = $item["To Date"] 
    	EligibilityCriteria = $item["Eligibility Criteria"] 
    	Exclusions = $item["Exclusions"] 
    	Addinfo = $item["Add Information"] 
    	ComplaintsHandling = $item["Complaints Handling"] 
    	MarketingCollateral= $item["Marketing Collateral"]
    	
    }
    
    $obj |select-object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral 
    $Results += $obj
    $Results | Export-Csv c:\Promo.csv -notype
    <br/>
    

    Instead of the colum being Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral
    I get

    ToDate  Exclusions  Title  MarketingCollateral  Addinfo EligibilityCriteria  Description  ComplaintsHandling  FromDate

    Whys is this..
    Any ideas

    Friday, July 09, 2010 6:35 AM

Answers

  • Hashtable elements are not guaranteed to be in any particular order (the Property switch to New-Object cmdlet takes a hashtable parameter). So you need to produce and object with properties in the right order with select-object cmdlet.

    Substitute this line

    $obj |select-object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    with the following one:

    $obj = $obj |select-object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    and try again.

    Friday, July 09, 2010 8:14 AM
  • Try this

    foreach ($item in $list.items)
    {
        New-Object PSObject -Property @{
            Title = $item["Title"]
            Description = $item["Description"]
            FromDate = $item["From Date"]
            ToDate = $item["To Date"]
            EligibilityCriteria = $item["Eligibility Criteria"]
            Exclusions = $item["Exclusions"]
            Addinfo = $item["Add Information"]
            ComplaintsHandling = $item["Complaints Handling"]
            MarketingCollateral= $item["Marketing Collateral"]
        } | Select-Object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    } | Export-Csv c:\Promo.csv -notype


    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Friday, July 09, 2010 10:21 AM
  • You can get around this limitation with a PSObject by using add-member rather than passing everything from a hash i.e.

    $obj = New-Object PSObject
    $obj |Add-Member -Name Title -MemberType NoteProperty -Value $item["Title"]
    $obj |Add-Member -Name Description -MemberType NoteProperty -Value $item["Description"]


    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    Monday, July 12, 2010 3:14 AM

All replies

  • Hashtable elements are not guaranteed to be in any particular order (the Property switch to New-Object cmdlet takes a hashtable parameter). So you need to produce and object with properties in the right order with select-object cmdlet.

    Substitute this line

    $obj |select-object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    with the following one:

    $obj = $obj |select-object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    and try again.

    Friday, July 09, 2010 8:14 AM
  • Try this

    foreach ($item in $list.items)
    {
        New-Object PSObject -Property @{
            Title = $item["Title"]
            Description = $item["Description"]
            FromDate = $item["From Date"]
            ToDate = $item["To Date"]
            EligibilityCriteria = $item["Eligibility Criteria"]
            Exclusions = $item["Exclusions"]
            Addinfo = $item["Add Information"]
            ComplaintsHandling = $item["Complaints Handling"]
            MarketingCollateral= $item["Marketing Collateral"]
        } | Select-Object Title,Description,FromDate,ToDate,EligibilityCriteria,Exclusions,Addinfo,ComplaintsHandling,MarketingCollateral

    } | Export-Csv c:\Promo.csv -notype


    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Friday, July 09, 2010 10:21 AM
  • You can get around this limitation with a PSObject by using add-member rather than passing everything from a hash i.e.

    $obj = New-Object PSObject
    $obj |Add-Member -Name Title -MemberType NoteProperty -Value $item["Title"]
    $obj |Add-Member -Name Description -MemberType NoteProperty -Value $item["Description"]


    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    Monday, July 12, 2010 3:14 AM