Today, I will show you an interesting topic related to the PowerShell switch statement. I will explain how to use wildcards in a PowerShell switch statement.
In PowerShell, you can use the -Wildcard parameter in a switch statement to match patterns with wildcards. For instance, switch -Wildcard ($value) { “apple” { Write-Output “This is an apple-based product.” } “pie” { Write-Output “This contains pie.” } } will match any string starting with “apple” or containing “pie”. To handle multiple values, you can provide an array of patterns, such as switch -Wildcard ($value) { @(“apple”, “banana”, “blueberry”) { Write-Output “This is a fruit-based product.” } }, enabling you to match various patterns efficiently.
Before using the -wildcard in a switch statement in PowerShell, here is a small example of how to use the switch statement.
$value = "apple"
switch ($value) {
"apple" { Write-Output "This is an apple." }
"banana" { Write-Output "This is a banana." }
default { Write-Output "Unknown fruit." }
}In this example, the switch statement checks the value of $value and executes the block of code associated with the matching case.
PowerShell Switch Wildcard Examples
Here, I will show you how to use the wildcard operator in a PowerShell switch statement.
Wildcards allow you to match patterns rather than specific values. In PowerShell, you can enable wildcard support in a switch statement using the -Wildcard parameter.
Example-1: Simple Wildcard
Let me show you a simple example where I have explained how to use a Wildcard (*) in a PowerShell Switch statement.
$productDescription = "Apple MacBook Pro"
switch -Wildcard ($productDescription) {
"Apple*" { Write-Output "This is an Apple product." }
"Microsoft*" { Write-Output "This is a Microsoft product." }
"Dell*" { Write-Output "This is a Dell product." }
default { Write-Output "Product category unknown." }
}In this example, the switch statement categorizes the product description based on the brand name.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Read PowerShell Switch Case with Regex
Example-2: Complex Wildcard Patterns
Now, I will show you a complex wildcard pattern. You can use more complex wildcard patterns to match different string parts here.
Here is the complete example.
$value = "apple pie with cream"
switch -Wildcard ($value) {
"apple*" { Write-Output "This is an apple-based product." }
"*pie*" { Write-Output "This contains pie." }
"*cream" { Write-Output "This ends with cream." }
default { Write-Output "Unknown product." }
}In this example, multiple patterns can match the value “apple pie with cream”. The output will be:
This is an apple-based product.
This contains pie.
This ends with cream.
Each matching case is executed, and this is the power of wildcards in switch statements.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Example-3: File Type Classification
To help you understand Wildcard, let me give you another example.
Suppose you want to classify files based on their extensions using wildcards in a switch statement.
$file = "report.pdf"
switch -Wildcard ($file) {
"*.txt" { Write-Output "This is a text file." }
"*.pdf" { Write-Output "This is a PDF document." }
"*.docx" { Write-Output "This is a Word document." }
"*.xlsx" { Write-Output "This is an Excel spreadsheet." }
default { Write-Output "Unknown file type." }
}In this example, the switch statement classifies the file based on its extension. Since $file is “report.pdf”, the output is “This is a PDF document.”
You can see the output in the screenshot below:

Read PowerShell Switch Statement with Multiple Conditions
PowerShell Switch Wildcard Multiple Values
Now, let us read some more advanced examples. Here, I will show you how to use PowerShell switch wildcard multiple values.
Sometimes, you may need to match multiple values with a single case. PowerShell allows you to do this by providing an array of patterns.
Example-1: Match Multiple Values with Wildcards
Here’s an example of how to match multiple values using wildcards in a PowerShell switch statement.
Here is the complete PowerShell script.
$value = "blueberry muffin"
switch -Wildcard ($value) {
{ $_ -like "apple*" -or $_ -like "banana*" -or $_ -like "blueberry*" } { Write-Output "This is a fruit-based product." }
{ $_ -like "*muffin" } { Write-Output "This is a muffin." }
default { Write-Output "Unknown product." }
}In this example, the first case matches any string that starts with “apple”, “banana”, or “blueberry”. Since $value is “blueberry muffin”, the output is “This is a fruit-based product.”
Once you execute the above PowerShell script using VS code, you can see the exact output, as shown in the screenshot below.

Example-2: Use Arrays for Complex Match with Wildcard
Now, let me show you how to use arrays to handle more matching conditions using wildcards.
Here is an example.
$value = "strawberry shortcake"
$patterns = @("apple*", "banana*", "blueberry*", "strawberry*")
switch -Wildcard ($value) {
{ $_ -like "apple*" -or $_ -like "banana*" -or $_ -like "blueberry*" -or $_ -like "strawberry*" } { Write-Output "This is a fruit-based product." }
"*cake" { Write-Output "This contains cake." }
default { Write-Output "Unknown product." }
}In this script:
- The first case uses a script block
{ $_ -like "apple*" -or $_ -like "banana*" -or $_ -like "blueberry*" -or $_ -like "strawberry*" }to check if$valuematches any of the specified fruit patterns. - The second case
"*cake"checks if the value contains “cake”. - The
defaultblock handles any value that does not match the previous patterns.
Here is the output in the screenshot below:

Read PowerShell Switch Parameter
Example-3: Combine Wildcards and Conditional Logic
In PowerShell, we can also combine wildcards with conditional logic to create more complex switch statements. Here’s an example:
$value = "chocolate cake"
switch -Wildcard ($value) {
"chocolate*" {
if ($value -like "*cake") {
Write-Output "This is a chocolate cake."
} else {
Write-Output "This is a chocolate product."
}
}
"*cake" { Write-Output "This is a cake." }
default { Write-Output "Unknown product." }
}In this example, the first case checks if the value starts with “chocolate”. If it does, an additional condition checks if the value also contains “cake”. Depending on the result, different messages are printed.
You can check the screenshot below for the output:

Example-4: Handle Multiple File Types with Wildcards
Now, let me show you another example of how to handle multiple file types with wildcards. You can also handle multiple file types with a single case using arrays.
Here is the complete script.
$file = "presentation.pptx"
$documentPatterns = @("*.pdf", "*.docx", "*.pptx")
switch -Wildcard ($file) {
{ $_ -like "*.pdf" -or $_ -like "*.docx" -or $_ -like "*.pptx" } { Write-Output "This is a document." }
"*.txt" { Write-Output "This is a text file." }
"*.xlsx" { Write-Output "This is an Excel spreadsheet." }
default { Write-Output "Unknown file type." }
}In this example, the $documentPatterns array contains patterns for different document types. The switch statement checks if $file matches any of these patterns. Since $file is “presentation.pptx”, the output is “This is a document.”
After you execute the above code, you can see the exact output like in the screenshot below:

Conclusion
In this tutorial, I explained how to use wildcards in a PowerShell switch statement with various examples. I have also shown some examples of PowerShell switch wildcard multiple values.
It is a bit tricky to use wildcards in a switch statement; if you still have questions, leave a comment below.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.