Do you want to check if an array contains a string in PowerShell? In this PowerShell tutorial, we’ll explore different methods to check if an array contains a string in PowerShell, providing real examples.
To check if an array in PowerShell contains a specific string, you can use the -contains
operator, which is straightforward and efficient. For example, $myArray -contains 'myString'
will return $true
if ‘myString’ is in $myArray
and $false
otherwise. Alternatively, for case-sensitive checks, you can use the .Contains()
method on the array: $myArray.Contains('myString')
.
An array is a data structure that holds a collection of items. In PowerShell, arrays can contain items of different types, including strings, integers, and even objects.
Here’s an example of how to create an array in PowerShell:
$myArray = @('apple', 'banana', 'cherry')
This array $myArray
contains three strings: “apple”, “banana”, and “cherry”.
Check if an Array Contains a String in PowerShell
Now, let us check if an array contains a string in PowerShell using different methods.
Method 1: Using the -contains Operator
The -contains
operator is the most straightforward method to check if an array contains a specific string. It returns $true
if the array contains the specified element, otherwise, it returns $false
.
Here is a complete example of how to check if an array contains a string in PowerShell using the -contains operator.
$fruits = @('apple', 'banana', 'cherry')
$searchString = 'banana'
if ($fruits -contains $searchString) {
Write-Host "The array contains the string $searchString."
} else {
Write-Host "The array does not contain the string $searchString."
}
I have executed the script using VS code, and you can see the output in the screenshot below.
Method 2: Using the .Contains() Method
Another way to check for the presence of a string in an array is to use the .Contains()
method of the array. This method is case-sensitive and will return $true
if the string is found in the array, and $false
otherwise.
Example: Check the below PowerShell to check if an array contains a string in PowerShell using the .Contains() method.
$fruits = @('apple', 'banana', 'cherry')
$searchString = 'Banana' # Note the capital 'B'
if ($fruits.Contains($searchString)) {
Write-Host "The array contains the string $searchString."
} else {
Write-Host "The array does not contain the string $searchString."
}
In this case, the output will indicate that the array does not contain the string “Banana” because .Contains()
is case-sensitive, and the array contains “banana” with a lowercase ‘b’.
Method 3: 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 value you are searching for followed by the -in
operator and the array.
Here is an example of checking if an array contains a substring using the -in operator in PowerShell.
$fruits = @('apple', 'banana', 'cherry')
$searchString = 'cherry'
if ($searchString -in $fruits) {
Write-Host "The array contains the string $searchString."
} else {
Write-Host "The array does not contain the string $searchString."
}
Method 4: Using the Where-Object Cmdlet
The Where-Object
cmdlet is a more versatile and powerful tool that can be used to filter arrays based on complex conditions in PowerShell. It can also check if an array contains a specific string.
Example:
$fruits = @('apple', 'banana', 'cherry')
$searchString = 'apple'
$containsString = $fruits | Where-Object { $_ -eq $searchString }
if ($containsString) {
Write-Host "The array contains the string $searchString."
} else {
Write-Host "The array does not contain the string $searchString."
}
Method 5: Using Regular Expressions with the -match Operator
For more complex pattern matching, you can use the -match
operator with regular expressions. This is particularly useful if you want to check for a string that matches a certain pattern rather than an exact string.
Example:
$fruits = @('apple', 'banana', 'cherry')
$searchPattern = 'b.n.n.'
if ($fruits -match $searchPattern) {
Write-Host "The array contains a string that matches the pattern $searchPattern."
} else {
Write-Host "The array does not contain any string that matches the pattern $searchPattern."
}
The string “banana” matches the regular expression pattern ‘b.n.n.’, so the output will indicate that the array contains a matching string.
Method 6: Using a Loop to Iterate Through the Array
Lastly, you can use a loop to iterate through each element in the array and check for the presence of the string in PowerShell. This method gives you complete control over the process and can be tailored for more complex conditions.
Here is a complete PowerShell script to check if an array contains a string using foreach loop.
$fruits = @('apple', 'banana', 'cherry')
$searchString = 'cherry'
$found = $false
foreach ($fruit in $fruits) {
if ($fruit -eq $searchString) {
$found = $true
break
}
}
if ($found) {
Write-Host "The array contains the string $searchString."
} else {
Write-Host "The array does not contain the string $searchString."
}
Once you run the script using VS code, you can see the output in the screenshot below.
Conclusion
PowerShell offers several methods to check if an array contains a specific string. Each method has its own use cases and can be chosen based on the specific needs of your script. For simple checks, -contains
, .Contains()
, and -in
operators are quick and easy. For more advanced filtering, Where-Object
and regular expressions with -match
are powerful. In this PowerShell tutorial, I have explained 5 different methods to check if an array contains a string in PowerShell.
You may also like:
- How to Replace String in PowerShell
- How to Replace a String in an Array Using PowerShell?
- PowerShell Get-Date To String
- How to Get Unique Values from an Array in PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com