0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check if String Contains Substring

Use the -like operator with wildcards like '*substring*' or the .Contains() method to check if a string contains a substring in PowerShell, for example: $string -like '*substring*' or $string.Contains('substring').
📋

Examples

Input'Hello World', 'World'
OutputTrue
Input'PowerShell scripting', 'script'
OutputTrue
Input'OpenAI ChatGPT', 'bot'
OutputFalse
🧠

How to Think About It

To check if one string contains another, think of it like looking for a word inside a sentence. You want to see if the smaller string appears anywhere inside the bigger string. In PowerShell, you can do this by using operators or methods that check for this presence directly.
📐

Algorithm

1
Get the main string and the substring to check.
2
Use a method or operator to test if the substring is inside the main string.
3
Return True if found, otherwise False.
💻

Code

powershell
$string = 'Hello World'
$substring = 'World'

if ($string -like "*${substring}*") {
    Write-Output 'True'
} else {
    Write-Output 'False'
}
Output
True
🔍

Dry Run

Let's trace the example where $string = 'Hello World' and $substring = 'World' through the code

1

Assign variables

$string = 'Hello World', $substring = 'World'

2

Check if $string contains $substring

Evaluate if 'Hello World' -like '*World*' which is True

3

Output result

Print 'True'

StepExpressionResult
2'Hello World' -like '*World*'True
💡

Why This Works

Step 1: Using -like operator

The -like operator checks if the string matches a pattern with wildcards, where * means any characters before or after the substring.

Step 2: Using .Contains() method

The .Contains() method returns True if the substring is found anywhere inside the string, otherwise False.

🔄

Alternative Approaches

Using .Contains() method
powershell
$string = 'Hello World'
$substring = 'World'

if ($string.Contains($substring)) {
    Write-Output 'True'
} else {
    Write-Output 'False'
}
This method is case-sensitive and straightforward for exact substring checks.
Using -match operator with regex
powershell
$string = 'Hello World'
$substring = 'World'

if ($string -match [regex]::Escape($substring)) {
    Write-Output 'True'
} else {
    Write-Output 'False'
}
This uses regular expressions and is powerful for pattern matching but slightly more complex.

Complexity: O(n) time, O(1) space

Time Complexity

Checking if a substring exists requires scanning the main string, which takes linear time proportional to the string length.

Space Complexity

No extra significant memory is needed; the check is done in-place with constant space.

Which Approach is Fastest?

The .Contains() method is generally faster and clearer for exact substring checks, while -like is simpler for wildcard patterns.

ApproachTimeSpaceBest For
-like with wildcardsO(n)O(1)Simple pattern matching with wildcards
.Contains() methodO(n)O(1)Exact substring presence, case-sensitive
-match with regexO(n)O(1)Complex pattern matching with regex
💡
Use -like '*substring*' for simple wildcard checks or .Contains() for exact substring presence.
⚠️
Forgetting to use wildcards * with -like causes the check to fail unless the string exactly matches.