Powershell - 5 ways to remove duplicates

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

How to remove duplicates in PowerShell?

Here are 5 techniques offering this possibility with or without case sensitivity.

# Delete duplicates with case sensitivity with Select-Object.
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Select-Object -Unique
# Delete case-sensitive duplicates with Sort-Object
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Sort-Object -Unique -CaseSensitive
# Delete duplicates with case sensitivity (Get-Unique requires a sorted list)
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Sort-Object | Get-Unique
# Remove case-sensitive duplicates with .net Linq
$array = 'C', 'A', 'B', 'C', 'c', 'C'
[Linq.Enumerable]::Distinct([string[]]@($array))
# Remove case-sensitive duplicates with a HashSet
$array = 'C', 'A', 'B', 'C', 'c', 'C'
[System.Collections.Generic.HashSet[string]]@($array)
# Delete case-insensitive duplicates with Sort-Object
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Sort-Object -Unique
# Remove case-insensitive duplicates with .net Linq.Enumerable
$array = 'C', 'A', 'B', 'C', 'c', 'C'
[Linq.Enumerable]::Distinct([string[]]@($array),[StringComparer]::InvariantCultureIgnoreCase)
# Remove case-insensitive duplicates with a HashSet
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$hashset = [System.Collections.Generic.HashSet[string]]::new([StringComparer]::InvariantCultureIgnoreCase)
$null = $array.ForEach( { $hashset.add( $_ ) } )
$hashset

Related links