PowerShell 7.3 - implementing Using Select-Object to Transform Nested Object Properties
I'm prototyping a solution and I'm trying to extract and transform properties from a collection of nested objects in PowerShell 7.3, but I'm running into difficulties using `Select-Object`. I have a list of custom user objects that contain nested properties, and I want to flatten these structures for easier reporting. Here's the code I'm currently using: ```powershell # Sample data creation $users = @( [PSCustomObject]@{ Name = 'Alice'; Details = @{ Age = 30; Country = 'USA' } }, [PSCustomObject]@{ Name = 'Bob'; Details = @{ Age = 25; Country = 'UK' } }, [PSCustomObject]@{ Name = 'Charlie'; Details = @{ Age = 28; Country = 'Canada' } } ) # Attempting to flatten the structure $flattenedUsers = $users | Select-Object Name, @{Name='Age';Expression={ $_.Details.Age }}, @{Name='Country';Expression={ $_.Details.Country }} $flattenedUsers ``` While the code seems correct, I receive the following behavior: ``` Select-Object : want to bind argument to parameter 'InputObject' because it is null. ``` I’ve confirmed that `$users` is populated correctly. I suspect the scenario may be due to how I'm accessing the nested properties, especially if any of the `Details` properties are null. I've tried adding checks to see if `Details` exists before trying to access `Age` or `Country`, but I still get errors. I've also considered using `ForEach-Object`, but that feels cumbersome for what should be a relatively simple transformation. What are the best practices for handling these types of nested properties in PowerShell? Is there a more efficient way to flatten these nested objects without running into null reference issues? This is part of a larger application I'm building. Any ideas what could be causing this?