The property order is not garantueed, unless you use the [ordered] tag that was introduced in PowerShell v3.
See my Export-Xlsx, the sequel, and ordered data post.
An example how this can be done.
$metrics="disk.numberwrite.summation","disk.numberread.summation","disk.read.average","disk.write.average"
$vms=Get-VM|where {$_.PowerState-eq"PoweredOn"}
Get-Stat-Entity$vms-Stat$metrics-maxsamples1-IntervalSecs20|
Group-Object-PropertyEntityId|Foreach-Object{
$row= [ordered]@{
VM=$_.Group[0].Entity.Name
}
$i=1
$_.Group|where {$_.Instance-ne""} |Group-Object-PropertyInstance|%{
$row.Add("Instance$i",$_.Values[0])
$_.Group|Group-Object-PropertyMetricId|%{
$row.Add("$($_.Values[0].Split('.')[1])$i",$_.Group[0].Value)
}
$i++
}
New-ObjectPSObject-Property$row
} |Sort-Object-Property {$_|Get-Member-MemberTypeNoteProperty|Measure-Object|Select-ExpandPropertyCount} -Descending|
Export-Csv"C:\report1.csv"-NoTypeInformation-UseCulture
The alternative is to use a Select-Object where you specify the properties in the order you want them before the Export-Csv.
But that would require that you know the names of all the properties, which is not obvious since we added them dynamically.