How to use PowerShell “Contains” – A Quick Guide!
The Contains
method in PowerShell can be used to check if a specified string or character exists within a string and the PowerShell operator Contains
allows you to check if a collection of objects, such as an array, contains a specific value. It returns a Boolean value, either $true
if the string or character is found, or $false
if it is not found. Here is a detailed guide on how to use the Contains
in PowerShell:
How to Use PowerShell “Contains” Function in String?
The PowerShell contains function allows you to check if a string contains a specific substring. This can be useful for finding specific text in a file or checking if a string contains a certain word or phrase. The PowerShell “Contains” function can be used by running the following command:
"Hello world".Contains("world")
In this example, we are checking if the string “Hello world” contains the substring “world”, and it returns “True”.
The Contains
operator can be used in conditional statements, such as if
statements, to check if a collection contains a specific value and take action based on the result. Here is an example of using the Contains
operator in an if
statement:
$string = "Hello, World!"
if ($string.Contains("World")) {
Write-Output "The string contains the word 'World'"
} else {
Write-Output "The string does not contain the word 'World'"
}
In this example, the Contains
method will return $true
because the string “Hello, World!” contains the word “World”.
How to use the Contains method for a case-insensitive search?
The contains method is case-sensitive by default! So, the “Hello world”.Contains(“WORLD”) returns False, as the case differs. To perform a case-insensitive check, you can convert and make sure both the string and substring to the same case using ToLower() or ToUpper() methods. E.g
"Hello World".ToUpper().Contains("WORLD")
Using Contains Operator with Arrays
You can also use the -Contains
Operator to check if an object is contained in an array or collection. For example:
#Array
$array = "apple", "banana", "mango"
#Check if array contains a string
if ($array -Contains "banana") {
Write-Output "The array contains the string 'banana'"
}
else {
Write-Output "The array does not contain the string 'banana'"
}
In this example, the -Contains
operator will return $true
because the string “banana” is contained in the array.
Also, The Contains
operator does not use strict equality comparison, meaning that it does not check if the value is exactly equal to the value in the collection. For example,
$Array = 1, 2, 3.0, 4, 5
$result = $Array -Contains 3
Write-Output $result
Would return True
as the $arrary
containing the value 3.0, as the values are equal. The -notcontains
can be used to negate the result of the Contains
operator.
#array
$Array = 1, 2, 3, 4, 5
#Checks if an array not contains a value
$Result = $Array -NotContains 5
Write-Output $Result
This would return False, as the array contains the given value: 5. If you want to perform a case-sensitive search, use the -CContains
operator. E.g.
#Array
$array = "apple", "banana", "mango"
#Check if array contains a string
$Array -CContains "Banana"
This returns False, as the case for “banana” and “Banana” is not the same.
Tail: The “IN” operator vs. Contains in PowerShell
The -in
operator checks if the value exists in an array (Whereas the Contains operator checks if an array contains a given value):
#array
$Array = 1, 2, 3, 4, 5
#Checks if a value exists in the Array
$Result = 4 -in $Array
Write-Output $Result
I hope this helps give you a basic understanding of how to use the Contains
method and operator in PowerShell.
Conclusion
In summary, the contains operator or method is a useful tool in PowerShell for checking if a collection of objects, such as an array or a string, contains a specific value. It has a simple syntax and can be used in a variety of situations to automate tasks and make your work more efficient. In this article, we have discussed the PowerShell contains function to check if a string contains a specific substring. We have also learned through some examples how to use this function.