Table of Contents
Using Select-Object
with the -Unique
Parameter
To remove duplicates from array in PowerShell:
- Use the array operator to create an array having duplicate values.
- Use the
Select-Object
with the-Unique
parameter to select unique values from the array we created in the previous step.
1 2 3 4 5 |
$array = @(2,2,4,4,3,3,6,9,8,9,1,0) $array = $array | Select-Object -Unique $array |
1 2 3 4 5 6 7 8 9 10 |
2 4 3 6 9 8 1 0 |
For this code, we used the array operator (@()
) to create an array having duplicate values and saved the array in the $array
variable using the assignment operator (=
). Note that, in PowerShell, the variables names are prefixed with a $
sign, for instance, $VariableName
.
Next, we used the Select-Object
cmdlet (it is pronounced as command-let
, a command used in Windows PowerShell) with the -Unique
parameter to select unique values from the given array. How? Let’s break down the $array = $array | Select-Object -Unique
command and understand it.
First, we specified the input array ($array
) and used pipe (|
) to send its results to the Select-Object
cmdlet. Here, the pipe was used to send the results of the preceding command to the following command.
Then, the Select-Object
cmdlet chooses the given properties of the object/set of objects; it can select the specified objects in a specific index in the array, a certain number of objects and unique objects.
We used this cmdlet with the -Unique
parameter to select unique values from the given array. After that, we used the assignment operator to update the original array.
Remember, the -Unique
parameter is case-sensitive, which means Hello
, and hello
would be considered unique values. Let’s see it in the following example containing string-type elements in the array.
1 2 3 4 5 |
$array = @("Hello", "hello", "Hi", "Greetings", "Hi") $array = $array | Select -Unique $array |
1 2 3 4 5 6 |
Hello hello Hi Greetings |
Did you see, we got Hello
and hello
, both. It is because the difference in character casing is considered as unique values for the -Unique
parameter with the Select-Object
cmdlet (whose alias is Select
).
Using Sort-Object
with -Unique
Parameter
To remove duplicates from an array in PowerShell:
- Use the array operator to create an array with duplicate values.
- Use the
Sort-Object
with the-Unique
parameter to remove duplicate values from the array we created in the first step.
1 2 3 4 5 |
$array = @(2,2,4,4,3,3,6,9,8,9,1,0) $array = $array | Sort-Object -Unique $array |
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 6 8 9 |
We’ve already learned about array creation using the array operator, variable declaration and initialization, assignment operator, pipe sign, selecting unique values and the execution flow of the cmdlet while using Select-Object
.
This code example is similar to the one we used while learning Select-Object
; here, we used the Sort-Object
cmdlet, which can sort the objects in descending or ascending order depending on the object property values. What does that mean?
It means if we do not specify the sort property with the Sort-Object
, then it will use the default sort property of the first input object. Remember, if there is no default sort property for the input object’s type, then PowerShell will compare the objects themselves. You can read more about it here
Here, properties will be sorted as case-insensitive or case-sensitive while working with string-type array elements. How? Let’s understand it using the -Unique
parameter with the Sort-Object
cmdlet below.
1 2 3 4 5 |
$array = @("Hello", "hello", "Hi", "Greetings", "Hi") $array = $array | Sort-Object -Unique -CaseSensitive $array |
1 2 3 4 5 6 |
Greetings hello Hello Hi |
The above code fence showed that the -Unique
parameter will consider Hello
and hello
as unique values if the -CaseSensitive
parameter is given. Also, the elements will be sorted in alphabetical order considering their first letter due by using the Sort-Object
cmdlet.
If you want case-insensitive results, omit the -CaseSensitive
parameter and use the following command.
1 2 3 4 5 |
$array = @("Hello", "hello", "Hi", "Greetings", "Hi") $array = $array | Sort-Object -Unique $array |
1 2 3 4 5 |
Greetings Hello Hi |
We can also use the Sort
as follows, an alias of the Sort-Object
cmdlet.
1 2 3 4 5 |
$array = @("Hello", "hello", "Hi", "Greetings", "Hi") $array = $array | Sort -Unique $array |
1 2 3 4 5 |
Greetings Hello Hi |
Further reading:
Using Get-Unique
with Sort-Object
To remove duplicates from an array in PowerShell:
- Use an array operator to create an array which contains duplicate values.
- Use
Sort-Object
to sort the given array andGet-Unique
to remove duplicate values from the specified array.
1 2 3 4 5 |
$array = @(2,2,4,4,3,3,6,9,8,9,1,0) $array = $array | Sort-Object | Get-Unique $array |
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 6 8 9 |
We have already learned about Sort-Object
in the previous section. Here, we used the Get-Unique
cmdlet with the Sort-Object
cmdlet to remove duplicate values from the given array.
You might be wondering why we are supposed to use the Get-Unique
cmdlet with the Sort-Object
cmdlet. It is because the Get-Unique
cmdlet selects unique values if the array is sorted. So, how the command $array = $array | Sort-Object | Get-Unique
was executed?
First, we specified the input array ($array
), and its results were forwarded to the Sort-Object
cmdlet using a pipe sign. Then, the Sort-Object
cmdlet sorted the input array in ascending order which was the default sort property of $array
. Afterwards, we used another pipe to forward the sorted array to the Get-Unique
cmdlet.
Now, the Get-Unique
cmdlet compared every element in the provided sorted array to the next, removed the identical values and returned one instance only for every element. Finally, sorted unique values were assigned to the $array
using the assignment operator (=
).
Just like the -Unique
parameter, the Get-Unique
is also case-sensitive, meaning strings with a difference in character casing will be considered unique values. See the following example for a demonstration.
1 2 3 4 5 |
$array = @("Hello", "hello", "Hi", "Greetings", "Hi") $array = $array | Sort-Object | Get-Unique $array |
1 2 3 4 5 6 |
Greetings Hello hello Hi |
That’s all about how to remove duplicates from Array in PowerShell.