Table of Contents
Using -Contains
Operator
Use the -Contains
operator to check if the list contains the specified string in PowerShell.
1 2 3 4 5 |
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") $searchString = "Wednesday" $list -Contains $searchString |
1 2 3 |
True |
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.
1 2 3 4 5 6 7 8 9 10 11 |
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") echo "Results using '-Contains':" $list -Contains "Wednesday" $list -Contains "wednesday" $list -Contains "WeDnesDay" echo "Results using '-iContains':" $list -iContains "Wednesday" $list -iContains "wednesday" $list -iContains "WeDnesDay" |
1 2 3 4 5 6 7 8 9 10 |
Results using '-Contains': True True True Results using '-iContains': True True True |
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.
1 2 3 4 5 6 |
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") $list -cContains "Wednesday" $list -cContains "wednesday" $list -cContains "WeDnesDay" |
1 2 3 4 5 |
True False False |
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 withi
andc
( 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.
1 2 3 4 5 6 7 8 9 10 |
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") $searchString = "Wednesday" $result = $list -cContains $searchString if($result){ Write-Host "'$searchString' is found." }else{ Write-Host "'$searchString' is not found." } |
1 2 3 |
'Wednesday' is found. |
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.
1 2 3 4 5 6 7 8 9 10 |
$list = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") $searchString = "Wednesday" $result = $list.Contains($searchString) if($result){ Write-Host "'$searchString' is found." }else{ Write-Host "'$searchString' is not found." } |
1 2 3 |
'Wednesday' is found. |
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 theContains()
method. However, we can useiContains
(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.
1 2 3 4 5 6 7 8 9 10 |
$fruitList = @("orange", "melon", "cherry", "banana") $searchPattern = "*an*" $matchedStrings = $fruitList | Where-Object {$_ -like $searchPattern} if ($matchedStrings.Count -gt 0) { Write-Host "List contains '$matchedStrings'." } else { Write-Host "List does not contain '$matchedStrings'." } |
1 2 3 |
List contains 'orange banana'. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$fruitList = @("orange", "melon", "cherry", "banana") echo "Match using -like Operator:" $fruitList | Where-Object {$_ -like "*an*"} $fruitList | Where-Object {$_ -like "*AN*"} $fruitList | Where-Object {$_ -like "*aN*"} echo "Match using -ilike Operator:" $fruitList | Where-Object {$_ -ilike "*an*"} $fruitList | Where-Object {$_ -ilike "*AN*"} $fruitList | Where-Object {$_ -ilike "*aN*"} echo "Match using -clike Operator:" $fruitList | Where-Object {$_ -clike "*an*"} $fruitList | Where-Object {$_ -clike "*AN*"} $fruitList | Where-Object {$_ -clike "*aN*"} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Match using -like Operator: orange banana orange banana orange banana Match using -ilike Operator: orange banana orange banana orange banana Match using -clike Operator: orange banana |
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.
1 2 3 4 5 6 7 8 9 10 |
$fruitList = @("orange", "melon", "cherry", "banana") $searchString = "orange" $match = $searchString -in $fruitList if ($match) { Write-Host "List contains '$searchString'." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
List contains 'orange'. |
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.
1 2 3 4 5 6 7 8 9 10 |
$fruitList = @("orange", "melon", "cherry", "banana") $searchString = "Orange" $match = $searchString -in $fruitList if ($match) { Write-Host "List contains '$searchString'." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
List contains 'Orange'. |
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.
1 2 3 4 5 6 7 8 9 10 |
$list = @("Mehvish", "Tania", "Divya") $searchString = "Divya" $index = $list.IndexOf($searchString) if ($index -ge 0) { Write-Host "'$searchString' found in list at index $index." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
'Divya' found in list at index 2. |
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.
1 2 3 4 5 6 7 8 9 10 |
$list = @("Mehvish", "Tania", "Divya") $searchString = "divya" $index = $list.IndexOf($searchString) if ($index -ge 0) { Write-Host "'$searchString' found in list at index $index." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
List does not contain 'divya'. |
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.
1 2 3 4 5 |
$list = @("Mehvish", "Tania", "Divya") $searchString = "Divya" $list.Where({$_ -eq $searchString}) |
<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.
1 2 3 4 5 |
$list = @("Mehvish", "Tania", "Divya") $searchString = "divya" $list.Where({$_ -eq $searchString}) |
1 2 3 |
Divya |
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.
1 2 3 4 5 6 7 8 9 |
$list = @("apple", "banana", "cherry") $searchString = "apple" if ([string]$list -match $searchString) { Write-Host "List contains '$searchString'." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
List contains 'apple'. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$list = @("apple", "banana", "cherry") $searchString = "apple" $matched = $false $list.ForEach({ if ($_ -eq $searchString) { $matched=$true } }) if ($matched) { Write-Host "List contains '$searchString'." } else { Write-Host "List does not contain '$searchString'." } |
1 2 3 |
List contains 'apple'. |
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 theForEach()
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#Create a list $list = New-Object System.Collections.Generic.List[string] #Add list items $list.Add("apple") $list.Add("orange") $list.Add("grapes") #returns True if string found $list -Contains "grapes" #returns True if string found $list.Contains("grapes") #returns matched string if found $list | Where-Object {$_ -like "gra*"} #returns True if string found "grapes" -in $list #returns index of the string, #if it is greater than or equal to 0, #then string is found $list.IndexOf("grapes") #returns matched string if found $list.Where({$_ -eq "grapes"}) #prints "string found", if it is found ForEach($item in $list){ if ($item -eq "grapes") { Write-Host "string found." } } #returns True if string found [string]$list -match "grapes" |
1 2 3 4 5 6 7 8 9 10 |
True True grapes True 2 grapes string found. True |
Using All Approaches with List Created Using ArrayList()
Use all of the above approaches with list created using ArrayList()
method in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#Create a list $list = New-Object System.Collections.ArrayList #Add list items $list.Add("apple") #returns 0 index $list.Add("orange") #returns 1 index $list.Add("grapes") #returns 2 index #returns True if string found $list -Contains "grapes" #returns True if string found $list.Contains("grapes") #returns matched string if found $list | Where-Object {$_ -like "gra*"} #returns True if string found "grapes" -in $list #returns index of the string, #if it is greater than or equal to 0, #then string is found $list.IndexOf("grapes") #returns matched string if found $list.Where({$_ -eq "grapes"}) #prints "string found", if it is found ForEach($item in $list){ if ($item -eq "grapes") { Write-Host "string found." } } #returns True if string found [string]$list -match "grapes" |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
0 1 2 True True grapes True 2 grapes string found. True |