REFERENCE SCRIPTS:
Script 1:
$csvList = @()
$csvList += New-Object PSObject -Property @{name="test1";accountname="testuser1";mail="user1@somewhere.com"}
$csvList += New-Object PSObject -Property @{name="test2";accountname="testuser2";mail="user2@somewhere.com"}
$csvList += New-Object PSObject -Property @{name="test3";accountname="testuser3";mail="user3@somewhere.com"}
$csvList += New-Object PSObject -Property @{name="test4";accountname="testuser4";mail="user4@somewhere.com"}
$csvList | Export-Csv c:\temp\testcsv.csv -NoTypeInformation
Script 2 (added in edit to reflect extended usage):
$aTest = @()
for($x=0;$x -le 5;$x++)
{
$aTest += New-Object PSObject -Property @{Name="test$($x)"; `
AccountName="testuser$($x)"; `
Mail="user$($x)@somewhere.com"}
}
$aTest | Export-Csv c:\temp\testcsv.csv -NoTypeInformation
QUESTION:
While that script creates my CSV and includes all the data I need in the correct rows, I cannot figure out how to control column position. Even though I'm ordering and adding the data by name,accountname,mail
Powershell orders it by mail,name,accountname
. How can I control the column order?
Note: If I do a screen dump of the contents of $csvList
before the export the order has already been changed.