question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return casesensitive hashtable with Group-Object

See original GitHub issue

Steps to reproduce

I know PowerShell is mostly case insensitive, but when using both -AsHashTable and -CaseSensitive I expected to end up with a case sensitive hashtable.

$capitonyms = @(
    [PSCustomObject]@{
        Capitonym = 'Bill'
    }
    [PSCustomObject]@{
        Capitonym = 'bill'
    }
)

$capitonyms | Group-Object Capitonym -AsHashTable -CaseSensitive

Expected behavior

Name                           Value
----                           -----
Bill                           {@{Capitonym=Bill}}
bill                           {@{Capitonym=bill}}

Actual behavior

Group-Object : The objects grouped by this property cannot be expanded because there is a key duplication. Provide a valid value for the property, and then try again.
At line:1 char:15
+ $capitonyms | Group-Object Capitonym -AsHashTable -CaseSensitive
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Group-Object], Exception
+ FullyQualifiedErrorId : The objects grouped by this property cannot be expanded because there is a key duplication. Provide a valid value for the property, and then try again.,Microsoft.PowerShell.Commands.GroupObjectCommand

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.3
PSEdition                      Core
GitCommitId                    7.0.0-preview.3
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mklement0commented, Aug 30, 2019

since this cmdlet was envisioned as grouping objects by .NET property name

Grouping happens by property value, not name, and given that omitting -AsHashtable already works exactly as advertised:

PS> $capitonyms | Group-Object Capitonym -CaseSensitive

Count Name                      Group
----- ----                      -----
    1 bill                      {@{Capitonym=bill}}
    1 Bill                      {@{Capitonym=Bill}}

so should the variant with -AsHashtable.

So I don’t see a breaking change here, only a bug fix.

Users who opt for combining -CaseSensitive with -AsHashtable will have to be aware that the resulting hashtable will have case-sensitive key lookups, unlike regular PowerShell hashtables, but since -CaseSensitive is an explicit opt-in, that seems reasonable.

2reactions
vexx32commented, Aug 30, 2019

I mean, if -CaseSensitive doesn’t already make it explicit, we’re kind of out of options for making it explicit. 😂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Group-Object (Microsoft.PowerShell.Utility)
Group-Object returns a table with one row for each property value and a column that ... and AsHashtable parameters to create a case-sensitive...
Read more >
Is there any way to convert case-sensitive hashtable into ...
It's not possible to convert a case-sensitive hash table into a case-insensitive one, the way around it is to recreate it:
Read more >
Group-Object - PowerShell
To create a calculated, property, create a hash table with an Expression key that specifies a string or script block value. -caseSensitive Make...
Read more >
Exploring Group-Object in PowerShell | How
Beginning in PowerShell 7, Group-Object can combine the CaseSensitive and AsHashtable parameters to create a case-sensitive hash table. The hash ...
Read more >
Group-Object - PowerShell Command - PDQ
Indicates that this cmdlet returns the group as a hash table. The keys of the hash table are the property values by which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found