There will be times you might need to check if an array contains more than one value in PowerShell. In this PowerShell tutorial, I will explain in detail how to check if an array contains more than one value in PowerShell.
To check if an array contains more than one value in PowerShell, you can evaluate the count of the array. Use the condition $array.Count -gt 1 to determine if the array has more than one element. If the result is $true, the array contains multiple values; if $false, it does not or is empty.
Check if an Array Contains More than One Value in PowerShell
An array is a data structure that holds a collection of items. These items can be of the same type or different types. In PowerShell, creating an array is as simple as assigning multiple values to a variable:
$array = 1, 2, 3, 4, 5The above code creates an array with five integer elements. Now, let us check the below methods to see if an array contains more than one value in PowerShell.
1. By Check the Array Size
The simplest way to check if an array contains more than one value is to check its size in PowerShell. You can do this using the Count property of an array:
$array = 1, 2, 3
if ($array.Count -gt 1) {
"Array contains more than one value."
} else {
"Array contains one or no values."
}In the above example, -gt is the greater than operator, and Count returns the number of elements in the array. If the array has more than one element, the condition evaluates to $true.
You can see the output in the screenshot below.

2. Using the Length Property
Similar to the Count property, you can use the Length property to determine the number of elements in the array in PowerShell.
$array = 'PowerShell', 'Scripting'
if ($array.Length -gt 1) {
"Array contains more than one value."
} else {
"Array contains one or no values."
}Both Length and Count properties will give you the total number of elements in the array.
3. By Checking for Specific Values
If you’re interested in checking for specific values within the array, you can use the -contains operator. This operator allows you to determine if an array includes a particular value:
$array = 'Windows', 'Linux', 'macOS'
if ($array -contains 'Linux') {
"Array contains the value 'Linux'."
} else {
"Array does not contain the value 'Linux'."
}The -contains operator is useful for checking the presence of a single value, but it doesn’t directly tell you if there are multiple values in the array.
4. By Using Custom Functions
For more advanced scenarios, you might want to create a custom function that encapsulates the logic for checking if an array contains more than one unique value. Here’s a simple function that does just that:
function Test-MultipleValues {
param(
[array]$Array
)
if ($Array.Count -gt 1 -and ($Array | Select-Object -Unique).Count -gt 1) {
return $true
}
return $false
}
# Usage
$array = 'A', 'A', 'B', 'C'
if (Test-MultipleValues -Array $array) {
"Array contains more than one unique value."
} else {
"Array contains one or no unique values."
}In this function, Select-Object -Unique is used to filter the array to unique values before counting.
Conclusion
Checking if an array contains more than one value in PowerShell is straightforward. You can use the Count or Length properties to check the size of the array, or you can create a custom function for more complex checks.
In this PowerShell tutorial, I have explained different methods to check if an array contains more than one value in PowerShell.
You may also like:
- Remove Duplicates from an Array in PowerShell
- How to Join Arrays in PowerShell?
- How to Sum All Numbers in an Array in PowerShell?
- PowerShell Array vs ArrayList
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.