diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs index 3b9a558663c..2759083121e 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Group-Object.cs @@ -516,9 +516,12 @@ protected override void EndProcessing() s_tracer.WriteLine(_groups.Count); if (_groups.Count > 0) { - if (AsHashTable) + if (AsHashTable.IsPresent) { - Hashtable hashtable = CollectionsUtil.CreateCaseInsensitiveHashtable(); + StringComparer comparer = CaseSensitive.IsPresent + ? StringComparer.CurrentCulture + : StringComparer.CurrentCultureIgnoreCase; + var hashtable = new Hashtable(comparer); try { if (AsString) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 index 09ddcbe74a0..ffca1e254ef 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Group-Object.Tests.ps1 @@ -173,6 +173,21 @@ Describe "Check 'Culture' parameter in order object cmdlets (Group-Object, Sort- $testCulture = "1049" } - {$testObject | Group-Object -Culture $testCulture } | Should -Not -Throw + { $testObject | Group-Object -Culture $testCulture } | Should -Not -Throw + } + + It 'should not throw a key duplication error with -CaseSensitive -AsHashtable' { + $capitonyms = @( + [PSCustomObject]@{ + Capitonym = 'Bill' + } + [PSCustomObject]@{ + Capitonym = 'bill' + } + ) + + $Result = $capitonyms | Group-Object -Property Capitonym -AsHashTable -CaseSensitive + $Result | Should -BeOfType [HashTable] + $Result.Keys | Should -BeIn @( 'Bill', 'bill' ) } }