Skip to content

Return casesensitive hashtable with Group-Object #10441

Closed

Description

@lahell

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

Activity

  1. added
    Issue-Question
    on Aug 24, 2019
  2. added
    Issue-Enhancement
    Up-for-Grabs
    and removed
    Issue-Question
    on Aug 24, 2019
  3. SteveL-MSFT commented on Aug 24, 2019

    @SteveL-MSFT
    Member

    Seems completely reasonable to me.

  4. BrucePay commented on Aug 29, 2019

    @BrucePay
    Collaborator

    Steve Lee (@SteveL-MSFT) This would be a breaking change. If there are no key conflicts, current users may depend on the case-insensitivity of the generated hashtable. (Also note - since this cmdlet was envisioned as grouping objects by .NET property name, the notion of two properties with the same name differing by case is not the common case.)

    lahell (@lahell) Given that this is a breaking change, what is the rational for doing this? That is, what scenarios are unblocked, what important thing can you do that you couldn't do before? Providing these scenarios helps the Committee make decisions about this sort of thing. Thanks!

  5. added
    Breaking-Change
    on Aug 29, 2019
  6. lahell commented on Aug 29, 2019

    @lahell
    Author

    Why would anyone use both CaseSensitive and AsHashTable if they need a case insensitive hashtable?

    I noticed this when grouping AD users by department to look for typos and was not sure if this was intentional behaviour.

    This is not a big deal for me. I got the hashtable I needed with the following code.

    $hashtable = New-Object System.Collections.Hashtable
    $capitonyms | foreach { $hashtable[$_.Capitonym] += ,$_ }
    $hashtable
    
  7. SteveL-MSFT commented on Aug 29, 2019

    @SteveL-MSFT
    Member

    Agree with Bruce Payette (@BrucePay) that it would be good to understand what the real world scenario that is impacted by this. However, this doesn't have to be a breaking change, it could be a new switch to make it explicit.

  8. vexx32 commented on Aug 30, 2019

    @vexx32
    Collaborator

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

  9. mklement0 commented on Aug 30, 2019

    @mklement0
    Contributor

    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.

  10. lahell commented on Aug 30, 2019

    @lahell
    Author

    My real world scenario was just using it to clean up human errors in department names.

    I guess this could be more of an issue on Linux. This simple test ends in key duplication error.

    $dir1 = New-Item -Name test -Type Directory
    $files1 = 1..3 | foreach { New-Item -Path $dir1.FullName -Name "file$_" -Type File }
    
    $dir2 = New-Item -Name TEST -Type Directory
    $files2 = 1..3 | foreach { New-Item -Path $dir2.FullName -Name "file$_" -Type File }
    
    Get-ChildItem -Recurse | group DirectoryName -AsHashTable
    
  11. lahell commented on Nov 22, 2019

    @lahell
    Author

    Thanks Rain Sallow (/u/ta11ow) (@vexx32), I ran a few tests in preview 6 and your fix works great.

    I did notice that adding a $null to the mix gives a misleading error message.

    $capitonyms = @(
        [PSCustomObject]@{
            Capitonym = 'Bill'
        }
        [PSCustomObject]@{
            Capitonym = 'bill'
        }
        [PSCustomObject]@{
            Capitonym = $null
        }
    )
    
    $capitonyms | Group-Object Capitonym -AsHashTable -CaseSensitive
    

    The code above will throw the error message:

    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.
    

    The InnerException is:

    System.ArgumentNullException: Key cannot be null. (Parameter 'key')
        at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
        at Microsoft.PowerShell.Commands.GroupObjectCommand.EndProcessing()
    
  12. iSazonov commented on Nov 22, 2019

    @iSazonov
    Collaborator

    lahell (@lahell) Please open new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-ChangeIssue-EnhancementResolution-FixedUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions