Do you need to retrieve unique values from a PowerShell array? This blog post will guide you through different methods to get unique values from an array in PowerShell.
To get unique values from an array in PowerShell, use the Select-Object cmdlet with the -Unique switch. For instance, $uniqueArray = $array | Select-Object -Unique will return an array of distinct elements, removing any duplicates from the original $array.
Get Unique Values from an Array in PowerShell
In PowerShell, arrays are can hold objects of any type, including integers, strings, and other arrays.
Here’s an example of a simple array in PowerShell:
$myArray = @(1,2,3,4,5,5,6,7,8,9,0,0)In this array, we have some duplicate values: 5 and 0 appear twice.
Now, let us check out how to get unique values from an array in PowerShell using various methods.
1- Using Select-Object with -Unique
The most straightforward method to remove duplicates and get unique values from an array in PowerShell is by using the Select-Object cmdlet with the -Unique switch. This cmdlet filters the array and returns each unique item only once.
Here’s how you can use this method:
$myArray = @(1,2,3,4,5,5,6,7,8,9,0,0)
$uniqueArray = $myArray | Select-Object -Unique
$uniqueArrayAfter executing this command, $uniqueArray will contain all the unique values from $myArray.
I had executed the PowerShell script using VS code, check out the screenshot below for the output:

2- Using Get-Unique Cmdlet
Another way to obtain unique values from a PowerShell array is by using the Get-Unique cmdlet. However, for Get-Unique to work correctly, the array must first be sorted because it compares adjacent items.
Here’s how you can use Get-Unique:
$myArray = @(1,2,3,4,5,5,6,7,8,9,0,0)
$sortedArray = $myArray | Sort-Object
$uniqueArray = $sortedArray | Get-Unique
$uniqueArrayFirst, we sort $myArray using Sort-Object, and then we pipe the sorted array into Get-Unique to retrieve the unique values.
Here is the output you can see in the screenshot below:

3- Using Custom Functions and Loops
In PowerShell, you can also create your own custom function to get unique values from a PowerShell array.
Here’s a simple example of such a function:
function Get-UniqueValues {
param($array)
$uniqueValues = @()
foreach ($item in $array) {
if ($item -notin $uniqueValues) {
$uniqueValues += $item
}
}
return $uniqueValues
}
$uniqueArray = Get-UniqueValues -array $myArrayIn this function, we iterate over each item in the array. We check if the item is not already in the $uniqueValues collection, and if it’s not, we add it. This method is more manual but allows you to add additional logic if needed.
You can see the output in the screenshot below:

Regardless of your chosen method, the output will be an array of unique values. You can verify this by outputting the $uniqueArray to the console:
$uniqueArrayThis will print all the unique values from the original array in PowerShell.
Conclusion
Retrieving unique values from an array in PowerShell can be accomplished using several methods. For most cases, using Select-Object with the -Unique switch is the most efficient. However, for scenarios where you need to sort the array first or apply more complex logic, Get-Unique or custom functions with loops are excellent alternatives.
In this PowerShell tutorial, I will explain, how to get unique values from an array in PowerShell using the below methods:
- Using Select-Object with -Unique
- Using Get-Unique Cmdlet
- Using Custom Functions and Loops
You may also like:
- Find Duplicates in an Array in PowerShell
- Remove Blank Lines from an Array in PowerShell
- Sort Array Alphabetically In PowerShell
- How to Check if an Array is Empty in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.