Skip to content

Attributes don't show up in array of hashtables #12728

@gabrielsroka

Description

@gabrielsroka

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:

$users = @()

$user1 = [ordered]@{first = 1; last = 2}
$user2 = [ordered]@{first = 3; last = 5; middle = 5}

$users += [pscustomobject]$user1
$users += [pscustomobject]$user2

# The `middle` attribute doesn't show up in any of these:
$users
$users | Out-GridView
$users | ConvertTo-Csv -NoTypeInformation 
$users | Export-Csv users.csv -NoTypeInformation

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions