PowerShell Check if List Contains String

PowerShell check if String contains List

Using -Contains Operator

Use the -Contains operator to check if the list contains the specified string in PowerShell.

In the above example, we used an array operator represented with @() to create a list, which we stored in the $list variable. Next, we initialized the $searchString variable with a string value we wanted to search in the $list.

Finally, we used the -Contains operator to determine if the $list contains the $searchString. If it is, we will get True as an output; otherwise, False. Note that the -Contains operator is case-insensitive, meaning it would treat "Wednesday", "wednesday", and "WeDnesDay" same.

In PowerShell, the -Contains and -iContains operators produce the same results because both perform case-insensitive matches. Here i denotes the insensitivity. Let’s see them in the following example.

See, we got True for all the matches. Now, think of a use case where we must perform a case-sensitive match. In that case, we would use the -cContains operator; see the following example.

We only got True for the first match because -cContains returned False for the "wednesday" and `"WeDnesDay" search strings.

In PowerShell, prefix the -Contains operator with i and c ( as -iContains and -cContains) to perform the case-insensitive and case-sensitive match. Remember, by default, the -Contains produces the same output as the -iContains operator.

Let’s make this code more reader-friendly by adding an if-else block to display meaningful messages. See the following example.

The above output seems more reader-friendly and easy to comprehend. To do this, we stored the Boolean value in the $result variable, which was returned by the $list -cContains $searchString expression.

After that, we used the if statement and passed the $result variable to it. If the value of $result was True, the if block would be executed; otherwise, the else block would be. Finally, we used the Write-Host cmdlet in the if and else blocks to display customized and meaningful messages on the PowerShell console.

Using Contains() Method

Use the Contains() method to assess if the list contains the specified string in PowerShell.

This code example is similar to the last code snippet, but we used the Contains() method this time. The Contains() method took the $searchString as a parameter and returned a Boolean value which we stored in the $result variable. The Boolean value can be True or False where True represents match is found while False denotes match is not found.

To make it easier to understand, we passed the $result to the if statement, which would only be executed if the value of $result would be True; otherwise, the Write-Host cmdlet within the else block would be executed.

The Contains() method performs a case-sensitive match by default, and we do not have a direct method to do a case-insensitive match using the Contains() method. However, we can use iContains (learned in the previous section), -ilike, and -like operators. We will learn -like and -ilike later in this article.

Using -like Operator

Use the -like operator to check if the list contains a string in PowerShell.

First, we created a list of fruits and stored it in the $fruitList variable. Next, we initialized the $searchPattern variable with the "*an*" pattern. This pattern means the matched string must contain an exactly while the * wildcard character shows any number of characters. We used * before and after the an, so the matched string will have an n number of characters before and after the an. Remember, the number of characters before and after the an may or may not be the same.

After that, we piped the $fruitList to the Where-Object cmdlet, which iterated over each item of the $fruitList and performed partial matching using the -like operator. The -like operator finds multiple occurrences of an item by performing partial matching, while wildcard patterns are used for flexible search. Here, $_ represents the current item in PowerShell.

We stored the matched strings in the $matchedStrings variable, which we passed to the if statement. In the if statement, we used the Count property of the $matchedStrings variable and checked if that is greater than 0 using the -gt operator (greater than the operator). If it is, we execute the Write-Host cmdlet from the if block to display a message saying the match is found; otherwise, the Write-Host from the else block will be executed, stating the match is not found.

Worth noting point is that the -like operator does a case-insensitive match by default, which is the same as the -ilike operator. Using -like and -ilike operators, we get the same output for the patterns "*an*", "*AN*" and "*aN*". On the other hand, -clike does a case-sensitive match. See the following example to understand the difference.

For the -like and -ilike operators, we got two matched values (orange and banana) thrice because the values were matched for "*an*", "*AN*" and "*aN*" patterns. On the other hand, the -clike returned two matched values (orange and banana) once because the match was found for the "*an*" pattern only.

Using -in Operator

Use the -in operator to check if the list contains the specified string in PowerShell.

This time we used the -in operator to determine if the $searchString is in the $fruitList. If it is, we will get True; otherwise, False. We stored this returned Boolean value in $match to pass it to the if statement. Now, the if statement will be executed if the value of the $match variable is True.

Note that the -in operator performs case-insensitive match, meaning if we initialize the $searchString with "Orange", it would still execute the if block and return the expected output. See the following example.

The -in operator will not work with the search patterns such as "*an*".

Using IndexOf()Method

Use the IndexOf() method to determine if the list contains a string in PowerShell.

For this example, we created a list having first names, and we stored this list in the $list variable. Next, we defined the $searchString variable and set its value to "Divya". After that, we chained the IndexOf() method with $list. This method took $searchString as a parameter and returned the index if the string match was found.

We stored the returned index in the $index variable. Then, the $index was passed to the if statement, where we used the -ge operator to determine if the value of the $index variable is greater than or equal to 0. If it is, we used Write-Host to print the matched string with the corresponding index; otherwise, the else block would be executed.

Note that the IndexOf() performs a case-sensitive match, which means we will get -1 if we specify "divya" instead of "Divya" as a search string. See the following example.

See, the match was not found this time. So the else block was executed as we got a -1 index.

Using Where() Method with a Filter

Use the Where() method with a filter to check if the list contains a string in PowerShell.

<pre title=-‘OUTPUT’>
Divya

For the above code snippet, we used the Where() method with a filter to determine if the current list element matched the value of $searchString. Suppose it matched; the $list.Where({$_ -eq $searchString}) returned it; otherwise, we would get nothing on the PowerShell Console.

What if we change the value of $searchString from "Divya" to "divya". See what happens below.

We still got the same output. Why? Because the -eq operator within the Where() method performs the case-insensitive comparison.

Using -match Operator

Use the -match operator to determine if the list contains a string in PowerShell.

This example is similar to the previous ones, but we used the -match operator this time. To use this operator, we had to convert the $list to string, or we can say we had to typecast the $list to string as [string]$list. If the converted string ([string]$list) matches the searchString, then the Write-Host from the if block will be executed. Otherwise, the Write-Host from the else block will display the specified message on the PowerShell console.

Using ForEach() Method

Use the ForEach() method to check if the list contains a string in PowerShell.

After setting the values of the $list and $searchString variables, we initialized the $matched with the $false value; this value will be used to represent whether the match was found or not. Next, we used the ForEach() method to iterate over all the list items. Finally, we used the if statement with the -eq operator for each list item to check if the current list item (represented by $_) equals the $searchString. If it is, then set the value of the $matched variable to $true.

Finally, we passed the $matched to another if statement outside the ForEach() method. If the value of $matched was True, then the if block will be executed; otherwise, the else block will be.

This code will produce the same output for the "apple" and "AppLe" search strings because the -eq operator inside the ForEach() method performs the case-insensitive comparison.

Using All Approaches with List Created Using List()

Use all of the above approaches with list created using List() method in PowerShell.

Using All Approaches with List Created Using ArrayList()

Use all of the above approaches with list created using ArrayList() method in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *