Recently, I got a requirement to find a string in a PowerShell array. In this PowerShell tutorial, I will explain in detail how to find a string in an array in PowerShell using various methods and examples.
To find a string in an array in PowerShell, use the -contains operator. If $array is your array and $string is the string you’re searching for, $array -contains $string will return $true if the string is in the array, otherwise $false. For pattern matching, you can use -match with a regular expression.
Find a String in an Array in PowerShell
In PowerShell, arrays can hold items of any type, including strings, integers, and objects. Here’s how you can create an array of strings in PowerShell:
$stringArray = @('apple', 'banana', 'cherry', 'date')Now, let’s look at different methods to find a string within this array.
1. Using the -contains Operator
The -contains operator is used to check if an array contains a specific element in PowerShell. Here’s an example:
$stringArray = @('apple', 'banana', 'cherry', 'date')
if ($stringArray -contains 'banana') {
"The array contains 'banana'."
} else {
"'Banana' was not found in the array."
}This will output:
The array contains 'banana'.Look at the screenshot below, I executed the script using VS code.

2. Using the -in Operator
The -in operator is similar to -contains, but the syntax is reversed. Instead of specifying the array first, you specify the element you’re looking for:
Here is a complete PowerShell script to find a string in a PowerShell array.
$stringArray = @('apple', 'banana', 'cherry', 'date')
if ('banana' -in $stringArray) {
"'Banana' is in the array."
} else {
"'Banana' is not in the array."
}The output will be the same as the -contains example.
3. Using the Where-Object Cmdlet
Where-Object is a more powerful cmdlet that allows you to specify complex search queries. Here’s how you can use it to find a string in an array in PowerShell.
$stringArray = @('apple', 'banana', 'cherry', 'date')
$result = $stringArray | Where-Object { $_ -eq 'banana' }
if ($result) {
"Found 'banana': $result"
} else {
"Did not find 'banana'."
}This will output:
Found 'banana': banana4. Using the Select-String Cmdlet
While Select-String is typically used to search text patterns in input strings and files, it can also be used to search within an array of strings. Here’s an example of finding a string in a PowerShell array.
$stringArray = @('apple', 'banana', 'cherry', 'date')
$result = $stringArray | Select-String -Pattern 'banana'
if ($result) {
"Found 'banana': $($result.Line)"
} else {
"Did not find 'banana'."
}5. Using the .IndexOf() Method
The .IndexOf() method is a method available on arrays that returns the index of the first occurrence of a specified value in an array. If the value is not found, it returns -1. Here’s how you can use it:
$stringArray = @('apple', 'banana', 'cherry', 'date')
$index = $stringArray.IndexOf('banana')
if ($index -ne -1) {
"'Banana' found at index: $index"
} else {
"'Banana' not found."
}This will output:
'Banana' found at index: 16. Using a Loop to Find a String
Sometimes, you might want to perform additional actions when you find a string in a PowerShell array. In such cases, a loop can be handy. Here’s an example using a foreach loop:
$stringArray = @('apple', 'banana', 'cherry', 'date')
foreach ($item in $stringArray) {
if ($item -eq 'banana') {
"Found 'banana': $item"
break
}
}This will output:
Found 'banana': bananaConclusion
Finding a string in an array is a common task in PowerShell scripting. In this PowerShell tutorial, I have explained how to find a string in an array in PowerShell using various methods like:
- Using the -contains Operator
- Using the -in Operator
- Using the Where-Object Cmdlet
- Using the Select-String Cmdlet
- Using the .IndexOf() Method
- Using a Loop to Find a String
Remember that PowerShell is case-insensitive by default, but if you need case-sensitive comparisons, you can use the c versions of the operators (e.g., -ceq, -ccontains, -cin).
You may also like:
- How to Find Duplicates in an Array in PowerShell?
- Get the First Element of an Array in PowerShell
- Split an Array into Smaller Arrays in PowerShell
- How to Check if an Array Contains Another Array 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.