Posts

Showing posts with the label pscustomobject

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 [PSCustomObjec...