Home » PowerShell » PowerShell String Contains to Check String

PowerShell String Contains to Check String

PowerShell has Contains() method and -contains operator to check if the PowerShell string contains the specified substring within the string.

The PowerShell string Contains() method checks if a string contains the specified string and It performs the case-sensitive comparison to check string contains another word and returns true if the string contains the value else returns false.

Contains (String) – Syntax

bool Contains(string value)

PowerShell -contains operator check if the collection matches the specified string. It is case-insensitive and doesn’t do string comparisons.

-contains Syntax

bool -contains string value

In this article, we will discuss how to use the PowerShell String Contains method and PowerShell contain operator to check if a string contains the word.

PowerShell String Contains

To check if a string contains another string value, use the PowerShell Contains method.

PowerShell String Contains method performs the case-sensitive string comparison to check if a string contains a specified substring.

Let’s consider, a string WelCome to ShellGeek Tutorials! , and we want to check if the given string contains the Shell word that occurs in it or not.

$str = "WelCome to ShellGeek Tutorials!"

$str.Contains("Shell") 

In the above PowerShell Script, the PowerShell Contains method takes a string value as an input parameter and checks the value with the $str string.

The example outputs a True value as it contains the string in it.

PowerShell String Contains Check
PowerShell String Contains Check

PowerShell Contains method performs a case-sensitive string comparison and returns the result.

Let’s consider the above to check the work shell in the given string.

Contains method takes a lowercase word shell to check if a string contains it.

Original string $str contains the Shell, hence string comparison will return false.

$str = "WelCome to ShellGeek Tutorials!"  
$str.Contains("shell")

# Result
False

In the above Powershell script, Contains() method checks for word shell and it returns false as it doesn’t find it.

To solve this case-sensitive string comparison check if a string contains a value inside the string, and use ToLower() method over the string object.

ToLower() method converts an entire string to lowercase so that it can be used while during the string comparison.

$str = "WelCome to ShellGeek Tutorials!"     

$str.ToLower().Contains("shell") 

In the above PowerShell script, we have used ToLower() method over the string object $str to convert it into lowercase and use the Contains method to check if the string contains the value.

It returns the True value after string comparison. It checks if variable $str contains a string “shell”.

PS C:\> $str = "WelCome to ShellGeek Tutorials!"                                                                        
PS C:\> 

PS C:\> $str.ToLower().Contains("shell")                                                                                
True
PS C:\> 

PowerShell if String contains Using Contains Operator

The PowerShell string contains operator checks the string contains the value. It checks for the entire value match in the given string and returns True if it is present else False.

In the following operator, the $str variable contains the string. We have used PowerShell contains operator to check if the given string contains the word ShellGeek.

PowerShell contains operator checks $str matches the entire value or not, it doesn’t contain the entire value, hence returns a False value.

$str = "WelCome to ShellGeek Tutorials!"
$str -contains "ShellGeek" 

The output of the above PowerShell script to check if the string contains a value is:

PS C:\> $str = "WelCome to ShellGeek Tutorials!"                                                                        
PS C:\>                                                                                                                 
PS C:\> $str -contains "ShellGeek"                                                                                      
False
PS C:\>                                                                                                                          

In such a case, use PowerShell like operator with a wildcard character (*) to check if a string contains a specified string. It searches if a string contains the substring in it or not

$str = "WelCome to ShellGeek Tutorials!"      
$str -like "*ShellGeek*" 

In the above PowerShell script, the $str variable contains the string data. To check if the string contains the specified string, we have used like operator with a wildcard character.

The output of the above PowerShell script checks if a variable containing a string returns a true value.

PS C:\> $str = "WelCome to ShellGeek Tutorials!"                                                                        
PS C:\>                                                                                                                 
PS C:\> $str -contains "ShellGeek"                                                                                      
False
PS C:\> $str -like "ShellGeek"                                                                                          
False
PS C:\> $str -like "*ShellGeek*"                                                                                        
True
PS C:\>                                                                                                                            

Conclusion

I hope the above article on how to use PowerShell Contains method and contains operator to check if the Powershell string contains a specified word is helpful to you.

If you want to use an operator, use a like operator with a wildcard character to check if the string contains a string.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Recommended Content

PowerShell String Contains Multiple Values