How to Get Unique Values from an Array in PowerShell?

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
$uniqueArray

After 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:

Get Unique Values from an Array in PowerShell

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
$uniqueArray

First, 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:

How to Get Unique Values from an Array in PowerShell

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 $myArray

In 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:

PowerShell Get Unique Values from an Array

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:

$uniqueArray

This 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:

  1. Using Select-Object with -Unique
  2. Using Get-Unique Cmdlet
  3. Using Custom Functions and Loops

You may also like: