PowerShell 7.3 - implementing Exporting Custom Objects to CSV with Nested Properties
Hey everyone, I'm running into an issue that's driving me crazy. I'm trying to export a collection of custom PowerShell objects to a CSV file, but I'm running into issues when the objects contain nested properties. The objects are created from a REST API response, and some of them have complex structures. For instance, here's how I create the objects: ```powershell $response = Invoke-RestMethod -Uri 'https://api.example.com/data' $data = $response | ForEach-Object { [PSCustomObject]@{ Name = $_.name Details = $_.details Address = [PSCustomObject]@{ Street = $_.address.street City = $_.address.city Zip = $_.address.zip } } } ``` When I try to export `$data` to a CSV file using `Export-Csv`, I get this warning: ``` Export-Csv : Input object want to be null, or an empty collection. ``` I've also tried flattening the objects like this: ```powershell $flattenedData = $data | ForEach-Object { [PSCustomObject]@{ Name = $_.Name Details = $_.Details Street = $_.Address.Street City = $_.Address.City Zip = $_.Address.Zip } } Export-Csv -Path 'output.csv' -NoTypeInformation ``` However, this results in missing values for nested properties if any entry is null, leading to empty columns in the CSV. Additionally, when I check the `$flattenedData` object, it appears to be populated correctly, but the final CSV output is inconsistent. How can I reliably export these nested properties into a CSV format without losing data or getting errors? Any guidance on how to handle this in PowerShell 7.3 would be greatly appreciated! Any help would be greatly appreciated!