Skip to content

GroupObject -AsHashTable should "core" any PSObjects being used as keys #6933

@BrucePay

Description

@BrucePay

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

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions