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
}
$citiesIn 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:

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"
$namesIn 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:

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()
$uniqueStatesThis 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:
- Get Unique Values from an Array in PowerShell
- Filter Unique Objects in PowerShell with Where-Object
- Add Value to an Array in a Function 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.