There is a bug when using the -AsHashTable flag on Group-Object such that the keys in the hashtable are still wrapped in PSObject when added to the table. This makes them useless because you can't retrieve the entry and confusing because it looks like a string but it isn't. See this issue on StackOverflow and also this one
Steps to reproduce
$groupedProjects = 1..10 | foreach {
[PSCustomObject]@{ ErrorMessage = if ($_ % 2) {'SomeError'} else {''} }
} |
Group-Object -Property {if ($_.ErrorMessage) {'Failed'} else {'Successful'}} -AsHashTable
$groupedProjects.Keys
$groupedProjects["Failed"]
Expected behavior
> $groupedProjects.Keys # the keys exist and look like strings
Successful
Failed
> $groupedProjects.Failed # Should be able to retrieve the corresponding value
ErrorMessage
------------
SomeError
SomeError
SomeError
SomeError
SomeError
>
Actual behavior
> $groupedProjects.Keys
Successful
Failed
> $groupedProjects.Failed # Specifying the key name fails because the actual key is a PSObject
>
Environment data
(This fails on all version of PowerShell)
> $PSVersionTable
Name Value
---- -----
PSVersion 6.1.0-preview.2
PSEdition Core
GitCommitId v6.1.0-preview.2
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
There is a bug when using the
-AsHashTableflag onGroup-Objectsuch that the keys in the hashtable are still wrapped inPSObjectwhen added to the table. This makes them useless because you can't retrieve the entry and confusing because it looks like a string but it isn't. See this issue on StackOverflow and also this oneSteps to reproduce
Expected behavior
Actual behavior
Environment data
(This fails on all version of PowerShell)