In short, how to convert this
$al = New-Object System.Collections.ArrayList
$al+=[PSCustomObject]@{prop1="value1"; prop2="value2"}
$al+=[PSCustomObject]@{prop1="value11"; prop3="value33"}
$al+=[PSCustomObject]@{prop4="value444"}
Into this
Use the psobject hidden memberset to discover all the property names before exporting:
$propertyNames = $al |ForEach-Object {
# Discover all the object's properties
$_.psobject.Properties |ForEach-Object {
# Get each property name
$_.Name
}
} |Sort-Object -Unique
# Now the CSV will have a column for every single unique property name
$al |Select-Object $propertyNames |Export-Csv -Path $Path1 -NoTypeInformation
#13906