In PowerShell, the array structure is designed to store a collection of items in a single variable. We can easily add elements of two different array objects using the plus (+) operator and store the result in the destination array. There may be a chance that both the arrays have the same value for some of the items before combining into a single array, in this case, we will have duplication in the destination array. We can use the Unique switch with Select-Object or Sort-Object cmdlet to remove duplicate values from an array.
In the following example, we combine the elements of two string arrays and remove duplicates using the Unique switch with Select-Object cmdlet.
$array1 = @('one', 'two','three')
$array2 = @('three','four','five')
$resultArray1 = $array1 + $array2
$resultArray1 # combined array with duplicates
$resultArray2 = $resultArray1 | Select-Object -Unique
$resultArray2 # combined array without duplicates
The Unique switch with Select-Object cmdlet performs case-sensitive string comparison, we need to use it with Sort-Object cmdlet to remove duplicates with case-insensitive check.
$array1 = @('one', 'two','three')
$array2 = @('two','Three','four')
($array1 + $array2) | Select-Object -Unique
Output - #Duplicates removed by case-sensitive check.
----
one
two
three
Three
Four
($array1 + $array2) | Sort-Object -Unique
Output - #Duplicates removed by case-insensitive check.
----
four
one
Three
Two
Join two custom object arrays and find distinct objects by property
When we work for real-time application needs, we must deal with a custom PowerShell object array instead simple string or integer array. The Unique switch can also be used to find distinct objects by property in a custom object array.
$array1 = @([PSCustomObject]@{Name = "User1"},[PSCustomObject]@{Name = "User2"})
$array2 = @([PSCustomObject]@{Name = "User2"},[PSCustomObject]@{Name = "User3"})
$resultArray = ($array1 + $array2) | Select-Object -Unique -Property Name
$resultArray # Result array with distinct Name values
Name
----
User1
User2
User3
As already explained, the Unique parameter with Select-Object cmdlet finds distinct values with case-sensitive string comparison. If you want to perform a case-insensitive operation, use the Unique parameter with the Sort-Object cmdlet.
$array1 = @([PSCustomObject]@{Name = "User1"},[PSCustomObject]@{Name = "user2"})
$array2 = @([PSCustomObject]@{Name = "User2"},[PSCustomObject]@{Name = "User3"})
# Find distinct objects by case-sensitive comparison.
($array1 + $array2) | Select-Object -Unique -Property Name
Name
----
User1
user2
User2
User3
# Find distinct objects by case-insensitive comparison.
($array1 + $array2) | Sort-Object -Unique -Property Name
Name
----
User1
User2
User3