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
Powershell - Simply send objects to different variables
Powershell - Tip - Simply send objects to different variablesPowershell - Testing network connectivity and port accessibility
Testing network connectivity and port accessibility with PowershellPowershell - Display network connections (equivalent to netstat)
Display network connections (listening ports, active connections...)Powershell - Testing name resolution (equivalent to nslookup)
Powershell commands to test name resolution (equivalent to nslookup)Powershell - View and manage DNS configuration of network interfaces
Powershell commands to display and manage DNS configuration of network interfacesPowershell - Managing IP configuration of network interfaces
Powershell commands to view and modify the IP configuration of network interfaces