PowerShell offers various methods to remove duplicates from an array.
Method 1: Use the Select-Object -Unique to remove duplicate items from an array
$uniqueArray = $array | Select-Object -Unique
$uniqueArray
This example returns the unique array after removing the duplicate elements from an array in PowerShell.
Method 2: Use the Sort-Object -Unique to remove duplicate items from an array
$uniqueArray = $array | Sort-Object -Unique
This example will remove the duplicate items while sorting the array.
Method 3: Use the Get-Unique to get a unique array
$sortedArray = $array | Sort-Object
$uniqueArray = $sortedArray | Get-Unique
This example uses the Get-Unique cmdlet to eliminate duplicates in a sorted array.
All of these methods in PowerShell remove the duplicates from an array.
The following examples show how to use each one of these methods.
Use the Select-Object -Unique to Remove Duplicate Items From an Array
To remove duplicates from an array in PowerShell, you can use the Select-Object cmdlet with the -Unique parameter.
The following example shows how to use it with syntax.
$languages = @('PowerShell','Java','Python','PowerShell','Python','JS') $uniqueArray = $languages | Select-Object -Unique $uniqueArray
Output:
PowerShell
Java
Python
JS
The output shows the duplicate items have been deleted from the array.
Use the Sort-Object -Unique to Remove Duplicate From an Array
Another way to remove duplicates from an array is by using the Sort-Object cmdlet with the -Unique parameter. It removes the duplicates while sorting the array.
The following example shows how to use it with syntax.
$languages = @('PowerShell','Java','Python','PowerShell','Python','JS') $uniqueArray = $languages | Sort-Object -Unique $uniqueArray
The output of the above PowerShell script is given below.
Java
JS
PowerShell
Python
Note that the array element is sorted and duplicate elements have been removed from an array.
Use the Get-Unique to Get Unique Array
The Get-Unique cmdlet in PowerShell is used to eliminate duplicates in an array or sorted list and returns a unique array.
The following example shows how to use it with syntax.
$languages = @('PowerShell','Java','Python','PowerShell','Python','JS') $sortedArray = $languages | Sort-Object $uniqueArray = $sortedArray | Get-Unique $uniqueArray
Output:
Java
JS
PowerShell
Python
This example returns the sorted unique array elements.
Conclusion
I hope the above article on how to remove duplicates from an array in PowerShell is helpful to you.
In most cases, the Select-Object -Unique should be sufficient to remove duplicates from arrays in PowerShell.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.