How to Add Only Unique Values to an Array in PowerShell?

While working with arrays in PowerShell, I was required to add only unique values. There are various methods to do so. In this tutorial, I will explain how to add unique values to an array in PowerShell with examples.

To add only unique values to an array in PowerShell, use a conditional check with the -contains operator. This method ensures a value is added only if it doesn’t already exist in the array. For example, given $cities = @(“New York”, “Los Angeles”, “Chicago”) and a new city $newCity = “Houston”, you can add the city with if (-not $cities -contains $newCity) { $cities += $newCity }. This approach prevents duplicates in your array efficiently.

Add Only Unique Values to an Array in PowerShell

Adding only unique values to an array prevents duplicates. This helps optimize scripts and avoid potential data issues.

There are various methods to add only unique values to an array in PowerShell. Let me show you each method with examples.

Method 1: Using a Conditional Check with -contains

The best way to add unique values to an array in PowerShell is by using a conditional check with the -contains operator. This method ensures that a value is added only if it doesn’t already exist in the array.

Syntax

Here is the syntax:

if (-not $array -contains $value) {
    $array += $value
}

Example

Let’s say we have an array of city names, and we want to add a new city only if it isn’t already in the list.

Here is the complete PowerShell script.

$cities = @("New York", "Los Angeles", "Chicago")
$newCity = "Houston"

if (-not $cities -contains $newCity) {
    $cities += $newCity
}

$newCity = "New York"

if (-not $cities -contains $newCity) {
    $cities += $newCity
}

$cities

In this example, “Houston” will be added to the $cities array, but “New York” will not be added again since it already exists in the array.

I executed the above script using VS code, and you can see the exact output in the screenshot below:

Add Only Unique Values to an Array in PowerShell

Check out Get Unique Values from CSV Using PowerShell

Method 2: Using a Custom Function

For more complex scenarios, you can create a custom function to add unique values to an array in PowerShell. This function can encapsulate the logic and be reused throughout your scripts.

Syntax

Here is the complete syntax:

function Add-UniqueValue {
    param (
        [array]$array,
        [object]$value
    )

    if (-not $array -contains $value) {
        $array += $value
    }

    return $array
}

Example

Here’s how you can use this function to manage a list of unique first names:

function Add-UniqueValue {
    param (
        [array]$array,
        [object]$value
    )

    if (-not $array -contains $value) {
        $array += $value
    }

    return $array
}

$names = @("James", "John", "Robert")
$names = Add-UniqueValue -array $names -value "Michael"
$names = Add-UniqueValue -array $names -value "James"
$names

In this example, “Michael” will be added to the $names array, but “James” will not be added again.

Here is the exact output in the screenshot below:

PowerShell Add Only Unique Values to an Array

Check out Get Unique Lines from a File Using PowerShell

Method 3: Using HashSet for Efficiency (PowerShell 5.0+)

Starting with PowerShell 5.0, the HashSet class from .NET can be used to manage unique values efficiently. HashSet automatically handles duplicates, making it a powerful tool for this task.

Syntax

Here is the syntax:

$hashSet = [System.Collections.Generic.HashSet[string]]::new()
$array | ForEach-Object { $hashSet.Add($_) }
$hashSet.Add($newValue)
$uniqueArray = $hashSet.ToArray()

Example

Let me show you an example:

Here’s how to use HashSet to add unique state abbreviations to an array:

$states = @("CA", "NY", "TX")
$hashSet = [System.Collections.Generic.HashSet[string]]::new()
$states | ForEach-Object { $hashSet.Add($_) }

$newState = "FL"
$hashSet.Add($newState)

$newState = "NY"
$hashSet.Add($newState)

$uniqueStates = $hashSet.ToArray()
$uniqueStates

This method ensures that “FL” is added to the $uniqueStates array, but “NY” is not added again.

Conclusion

There is no direct way to add only unique values to an array in PowerShell, but there are different ways to achieve this. In this tutorial, I explained how to do so, using a conditional check with -contains, a custom function, etc.

You may like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.