diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs index bcf7e31ccf130..09bc78d96933a 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUnique.cs @@ -50,6 +50,13 @@ public SwitchParameter OnType } private bool _onType = false; + + /// + /// Gets or sets case insensitive switch for string comparison. + /// + [Parameter] + public SwitchParameter CaseInsensitive { get; set; } + #endregion Parameters #region Overrides @@ -77,7 +84,7 @@ protected override void ProcessRecord() if (string.Equals( inputString, _lastObjectAsString, - StringComparison.CurrentCulture)) + CaseInsensitive.IsPresent ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture)) { isUnique = false; } @@ -89,9 +96,9 @@ protected override void ProcessRecord() else // compare as objects { _comparer ??= new ObjectCommandComparer( - true, // ascending (doesn't matter) + ascending: true, CultureInfo.CurrentCulture, - true); // case-sensitive + caseSensitive: !CaseInsensitive.IsPresent); isUnique = (_comparer.Compare(InputObject, _lastObject) != 0); } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs index 1c45fea699d3c..cf39a20a0688d 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Select-Object.cs @@ -110,6 +110,13 @@ public SwitchParameter Unique private bool _unique; + /// + /// Gets or sets case insensitive switch for string comparison. + /// Used in combination with Unique switch parameter. + /// + [Parameter] + public SwitchParameter CaseInsensitive { get; set; } + /// /// /// @@ -632,7 +639,11 @@ private void FilteredWriteObject(PSObject obj, List addedNotePro bool isObjUnique = true; foreach (UniquePSObjectHelper uniqueObj in _uniques) { - ObjectCommandComparer comparer = new(true, CultureInfo.CurrentCulture, true); + ObjectCommandComparer comparer = new( + ascending: true, + CultureInfo.CurrentCulture, + caseSensitive: !CaseInsensitive.IsPresent); + if ((comparer.Compare(obj.BaseObject, uniqueObj.WrittenObject.BaseObject) == 0) && (uniqueObj.NotePropertyCount == addedNoteProperties.Count)) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 index fa859c9b91bbc..037120c107428 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Unique.Tests.ps1 @@ -1,8 +1,12 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. Describe "Get-Unique DRT Unit Tests" -Tags "CI" { - It "Command get-unique works with AsString switch" { + + BeforeAll { $inputArray = "aa","aa","Aa","ba","BA","BA" + } + + It "Command get-unique works with AsString switch" { $results = $inputArray | Get-Unique -AsString $results.Length | Should -Be 4 @@ -17,6 +21,18 @@ Describe "Get-Unique DRT Unit Tests" -Tags "CI" { $results[2] | Should -BeOfType System.String $results[3] | Should -BeOfType System.String } + + It "Command get-unique works with AsString and CaseInsensitive switches" { + $results = $inputArray | Get-Unique -AsString -CaseInsensitive + + $results.Length | Should -Be 2 + + $results[0] | Should -BeExactly "aa" + $results[1] | Should -BeExactly "ba" + + $results[0] | Should -BeOfType System.String + $results[1] | Should -BeOfType System.String + } } Describe "Get-Unique" -Tags "CI" { @@ -26,6 +42,8 @@ Describe "Get-Unique" -Tags "CI" { $expectedOutput1 = 1,2,3,4,5 $collection = "a", "b", "b", "d" $expectedOutput2 = "a", "b", "d" + $collection2 = "a","A", "b", "B" + $expectedOutput3 = "a", "b" } It "Should be able to use the Get-Unique cmdlet without error with inputObject switch" { @@ -61,4 +79,9 @@ Describe "Get-Unique" -Tags "CI" { $actual = $collection | Get-Unique $(Compare-Object $actual $expectedOutput2 -SyncWindow 0).Length | Should -Be 0 } + + It "Should get the unique strings when CaseInsensitive switch is used" { + $actual = $collection2 | Get-Unique -CaseInsensitive + $(Compare-Object $actual $expectedOutput3 -SyncWindow 0).Length | Should -Be 0 + } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 index 042844534352e..d60795cbec35d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Select-Object.Tests.ps1 @@ -58,6 +58,13 @@ Describe "Select-Object" -Tags "CI" { $result | Should -Be $expected } + It "Should work work correctly with Unique and CaseInsensitive parameter" { + $result = "abc", "Abc" | Select-Object -Unique -CaseInsensitive + + $result.Count | Should -Be 1 + $result | Should -Be "abc" + } + It "Should return correct object with Skip parameter" { $result = $dirObject | Select-Object -Skip $TestLength