Export data in the order it was added - PowerShell Export-Csv


Export data in the order it was added - PowerShell Export-Csv



I have this code


function get-data()
{
$rec=[PSCustomObject]@()
$DLGP = "" | Select "Name","Grade","Score"

foreach($record in $data)
{
$DLGP.Name=$record.name
$DLGP.Grade=$record.grade
$DLGP.Score=$record.score
$rec += $DLGP
}
return $rec
}

$mydata=get-data
$mydata | Export-Csv -Path $outputPath -NoTypeInformation



The problem is, the data is not exported in the order that I have added it to $rec



How can I get it exported in the order it was added?





By default a [PSCustomObject] is unordered. Since Powerschell version 4 or 5 I guess there are ordered custom objects. Just add [ordered] in front of the [PSCutomObject] .... like this: [ordered][PSCostomObject]
– Olaf
Jun 30 at 8:36


[PSCustomObject]


[ordered]


[PSCutomObject]


[ordered][PSCostomObject]





@Olaf: A [pscustomobject] instance has always enumerated its properties in definition order. It is hashtables whose entries are enumerated in no guaranteed order; PSv3 introduced [ordered] @{ ... } to allow defining ordered hashtable literals whose entries are enumerated in definition order too and also enabled casting hashtable literals to [pscustomobject], in which case [ordered] is implied - in PSv2- such casts simply didn't work at all (were no-ops). However, the OP's issue is unrelated to member ordering - see my answer. [ordered] only ever works before @{ ... }.
– mklement0
2 days ago



[pscustomobject]


[ordered] @{ ... }


[pscustomobject]


[ordered]


[ordered]


@{ ... }




2 Answers
2



Without even using your function, a simple


$mydata = $data | select Name,Grade,Score



would yield the desired result.



Your function is IMO overcomplicated:


$Data = @"
Name,Grade,Score,Dummy
Anthoni,A,10,v
Brandon,B,20,x
Christian,C,30,y
David,D,40,z
"@ | ConvertFrom-Csv

function get-data() {
ForEach($record in $data) {
[PSCustomObject]@{
Name =$record.name
Grade=$record.grade
Score=$record.score
}
}
}

$mydata=get-data
$mydata # | Export-Csv -Path $outputPath -NoTypeInformation



Returns here the pretty same order:


Name Grade Score
---- ----- -----
Anthoni A 10
Brandon B 20
Christian C 30
David D 40



LotPings' helpful answer provides effective solutions.



As for what you tried:



By constructing only a single [pscustomobject] instance outside the loop:


[pscustomobject]


$DLGP = "" | Select "Name","Grade","Score"



and then only updating that one instance's properties in each loop iteration:


$DLGP.Name=$record.name
# ....



you effectively added the very same [pscustomobject] instance multiple times to the result array instead of creating a distinct object in each iteration.


[pscustomobject]



Since the same object was being updated repeatedly, that object ended up having the properties of the last object in the input collection, $data.


$data



As an aside:



[PSCustomObject] @() is effectively the same as @(): the [PSCustomObject] is ignored and you get an [object] array.


[PSCustomObject] @()


@()


[PSCustomObject]


[object]



To type the array as one containing [PSCustomObject] instances, you'd have to use an array-typed cast: [PSCustomObject] @().


[PSCustomObject]


[PSCustomObject] @()



However, given that instances of any type can be cast to [PSCustomObject] - which is really the same as [psobject] - this offers no type safety and no performance benefit.


[PSCustomObject]


[psobject]



Also, since your $rec variable isn't type-constrained (it is defined as $rec = [<type>] ... rather than [<type>] $rec = ...) and you're using += to "add to" the array (which invariably requires creation of a new instance behind the scenes), $rec would revert to an [object] array after the first += operation.


$rec


$rec = [<type>] ...


[<type>] $rec = ...


+=


$rec


[object]


+=






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Extract Id from Twitch Clip URL

Joe Torre

2