SolvedPowerShell Attributes don't show up in array of hashtables
✔️Accepted Answer
No, this is by design for this cmdlet. Isn't it in the documentation that the properties on the first object are used to determine the table headers?
Think about it. CSV requires that all lines have the same number of columns and that the column headings be listed at the top of the file.
Short of caching all the objects in memory and iterating the entire collection at least twice before writing the file, there's no way to ensure you haven't missed any properties and don't have a mismatched number of columns on some lines.
It would be extremely bad on performance for large CSV files to try to handle this in any other way imo.
If you have a set of objects that you know the full set of properties for, you can workaround this by using select-object to ensure that all the objects have the same set of properties before exporting them.
$objects |
Select-Object -Property $PropertyNames |
Export-Csv -Path $filepath
I tried the script Dr. Scripto wrote about in 2013:
https://devblogs.microsoft.com/scripting/write-users-and-proxy-addresses-to-csv-by-using-powershell/
but it didn't work. Perhaps it's no surprise since it was written many years ago.
It seems that PowerShell doesn't like this:
But if you add
$user2
first, it works fine.I have a script that calls an API hundreds of times, and will get different user attributes each time.
I can find a workaround, but doesn't this seem like a bug?
Thanks!