How to Check if a String Exists in an Array in PowerShell?

Recently, I got a task to check if a specific string exists within a PowerShell array. In this PowerShell tutorial, I will explain several methods to check if a string is a member of an array in PowerShell.

In PowerShell, to check if a string exists in an array, you can use the -contains operator. For instance, if $array is your array and $string is the string you’re looking for, you would write the condition $array -contains $string. This will return $true if the string is found in the array, and $false otherwise

How to Check if a String Exists in an Array in PowerShell

In PowerShell, you can create an array by assigning multiple values to a variable, separated by commas.

$array = "apple", "banana", "cherry"

Now let’s look at different ways to check if a string exists in this array in PowerShell using different methods.

Method 1: Using the -contains Operator

The -contains operator is the most straightforward approach to check for the presence of a string in an array in PowerShell. It returns $true if the array contains the specified value, otherwise, it returns $false.

Here is a complete example of “PowerShell: Check if a String Exists in an Array”.

$array = "apple", "banana", "cherry"
$stringToCheck = "banana"
$result = $array -contains $stringToCheck
Write-Host "Does the array contain the string? $result"

Here, you can see, that I have executed the PowerShell script using Visual Studio code, the output is like in the screenshot below:

Check if a String Exists in an Array in PowerShell

Method 2: Using the .Contains() Method

The .Contains() method in PowerShell is another way to check for the existence of an element in an array. It’s a method that comes from the underlying .NET framework, and it works similarly to the -contains operator.

Here is the complete PowerShell script.

$array = "apple", "banana", "cherry"
$stringToCheck = "banana"
$result = $array.Contains($stringToCheck)
Write-Host "Does the array contain the string? $result"

Once you execute the PowerShell script, you can see the output in the screenshot below.

PowerShell Check if a String Exists in an Array

Method 3: Using the -in Operator

The -in operator is like the reverse of the -contains operator in PowerShell. Instead of $array -contains $stringToCheck, you write $stringToCheck -in $array. It returns $true if the string is in the array, and $false otherwise.

Here is the complete script to check if a String Exists in an Array in PowerShell.

$array = "apple", "banana", "cherry"
$stringToCheck = "banana"
$result = $stringToCheck -in $array
Write-Host "Is the string in the array? $result"

Method 4: Using ForEach-Object with -match Operator

If you want to perform a more complex check, such as a case-insensitive search or partial matching, you can use ForEach-Object with the -match operator in PowerShell.

Here is the complete PowerShell script.

$array = "apple", "banana", "cherry"
$stringToCheck = "Ban"
$result = $array | ForEach-Object { $_ -match $stringToCheck }
$result = $result -contains $true
Write-Host "Does the array contain the string (case-insensitive)? $result"

In this example, -match is used for a case-insensitive partial match, and it will return $true because “banana” contains “Ban”.

Method 5: Using Custom Functions

You might want to define a custom function for more complex scenarios or repeated checks. Here is the complete script to Check if a String Exists in a PowerShell Array.

function Test-StringInArray {
    param(
        [string[]]$array,
        [string]$stringToCheck
    )
    return $array -contains $stringToCheck
}

$array = "apple", "banana", "cherry"
$stringToCheck = "banana"
$result = Test-StringInArray -array $array -stringToCheck $stringToCheck
Write-Host "Does the array contain the string? $result"

This function encapsulates the logic for checking if a string exists in an array, making your scripts cleaner and more reusable.

Here is the output in the below screenshot, after I executed it using VS code.

How to Check if a String Exists in an Array in PowerShell

Conclusion

Checking if a string exists in an array is a common task in PowerShell scripting. We’ve explored several methods to accomplish this, from simple operators like -contains and .Contains() method to more complex approaches with custom functions and partial matching.

In this PowerShell tutorial, I have explained how to check if a string exists in an array in PowerShell using various methods and real examples.

You may also like: