PowerShell Switch String Contains [With Examples]

One of my team members recently searched for implementing contains in the PowerShell switch statement. This is a very important concept that PowerShell developers use. In this tutorial, I will explain everything about “PowerShell switch string contains” with examples.

To check if a string contains a specific substring using a PowerShell switch statement, you can utilize the -Wildcard operator. For example, given a list of file names, you can categorize them based on their extensions with a switch statement like this:

$files = @("report.pdf", "data.csv", "presentation.pptx", "notes.txt")

foreach ($file in $files) {
    switch -Wildcard ($file) {
        '*.pdf' { Write-Output "$file is a PDF document." }
        '*.csv' { Write-Output "$file is a CSV file." }
        '*.pptx' { Write-Output "$file is a PowerPoint presentation." }
        '*.txt' { Write-Output "$file is a Text file." }
        default { Write-Output "$file type is unknown." }
    }
}

This approach allows you to match patterns within strings and execute corresponding actions efficiently.

PowerShell Switch String Contains

The switch statement in PowerShell is a conditional logic statement used to evaluate one or more conditions. It’s similar to a series of if statements but offers a cleaner and more readable syntax. The switch statement lists each condition and the corresponding action to execute when that condition is met.

The basic syntax of a switch statement in PowerShell is straightforward:

switch ($variable) {
    'condition1' { action1 }
    'condition2' { action2 }
    default { defaultAction }
}

When you want to check if a string contains a specific substring, you can use the -contains operator within the switch statement.

Let me show you how to do this.

I will show you how to use the switch statement to check if a string contains a specific substring. This is particularly useful when you’re dealing with file names, log entries, or any textual data where pattern matching is required.

Example 1: File Type Detection

Suppose you have a list of file names and need to categorize them based on their extensions. Here’s how you can do it with a switch statement and string contains:

$files = @("report.pdf", "data.csv", "presentation.pptx", "notes.txt")

foreach ($file in $files) {
    switch -Wildcard ($file) {
        '*.pdf' { Write-Output "$file is a PDF document." }
        '*.csv' { Write-Output "$file is a CSV file." }
        '*.pptx' { Write-Output "$file is a PowerPoint presentation." }
        '*.txt' { Write-Output "$file is a Text file." }
        default { Write-Output "$file type is unknown." }
    }
}

In this example, the -Wildcard operator allows the switch statement to match patterns within the file names.

You can see the output in the screenshot below:

powershell switch string

Check out Wildcards in PowerShell Switch

Example 2: Log Entry Filtering

Suppose you’re analyzing log entries and want to filter out entries containing specific keywords. Here’s how, and below is the complete PowerShell script for Switch String Contains.

$logEntries = @(
    "Error: Disk space low on server01",
    "Warning: High memory usage on server02",
    "Info: Backup completed successfully on server03",
    "Error: Network timeout on server04"
)

foreach ($entry in $logEntries) {
    switch -Regex ($entry) {
        'Error' { Write-Output "Critical Issue: $entry" }
        'Warning' { Write-Output "Warning: $entry" }
        'Info' { Write-Output "Information: $entry" }
        default { Write-Output "Uncategorized: $entry" }
    }
}

Here, the -Regex operator is used to match regular expressions within the log entries, allowing for more flexible pattern matching.

I executed the above PowerShell script using VS code and you can see the exact output in the screenshot below:

powershell switch string contains

Example 3: Multiple Conditions

You can handle multiple conditions within a single case by grouping them together. Here is a complete example for “PowerShell Switch String Contains”.

$input = "New York"

switch ($input) {
    {$_ -contains "New"} { Write-Output "This contains 'New'." }
    {$_ -contains "York"} { Write-Output "This contains 'York'." }
    default { Write-Output "No match found." }
}

In this example, the $_ variable represents the current item being evaluated, and -contains checks if the substring is present.

Check out PowerShell Switch Parameter

Example 4: Case-Insensitive Matching

PowerShell switch statements are case-sensitive by default, but you can make them case-insensitive by using the -CaseSensitive parameter:

$city = "Seattle"

switch -CaseSensitive ($city) {
    "seattle" { Write-Output "Matched 'seattle'." }
    default { Write-Output "No match found." }
}

Since Seattle and seattle are treated differently, the default case will execute.

Here is the output in the screenshot below:

powershell switch string contains examples

Conclusion

In this tutorial, I explained how to work with PowerShell switch statements with string contains and provided different real examples related to “PowerShell Switch String Contains.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.