How to Find a String in an Array in PowerShell?

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.

Find a String in an Array in PowerShell

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': banana

4. 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: 1

6. 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': banana

Conclusion

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:

  1. Using the -contains Operator
  2. Using the -in Operator
  3. Using the Where-Object Cmdlet
  4. Using the Select-String Cmdlet
  5. Using the .IndexOf() Method
  6. 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: