SoFunction
Updated on 2025-03-02

PowerShell Contains function to find string instances

This article introduces the use of the Contains function of strings in PowerShell to query whether another string exists in a string.

The Contains() function is inherited from a String object and can be directly used for string search and judgment. The return value of the Contains() function is a Boolean value, namely True or False, which means that it exists or does not exist.

As an example:
There is 1 in "123"

Copy the codeThe code is as follows:
PS C:\Users\spaybow> "123".contains("1")
True

"123" exists "12"
Copy the codeThe code is as follows:
PS C:\Users\spaybow> "123".contains("12")
True

There are 123 in "123"
Copy the codeThe code is as follows:
PS C:\Users\spaybow> "123".contains("123")
True

"123" does not exist "13"
Copy the codeThe code is as follows:
PS C:\Users\spaybow> "123".contains("13")
False

Because it is just whether it exists or not, the Contains function cannot locate the position of the query string in the query string. If you need to locate its position, you need to use the IndexOf function, which we will introduce soon.

This article introduces so much about PowerShell using the Contains function to find strings. I hope it will be helpful to you. Thank you!