0
0
PowerShellscripting~30 mins

Switch with wildcard and regex in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Switch with wildcard and regex
📖 Scenario: You are creating a simple script to categorize file names based on their patterns. This helps you quickly identify file types and special cases.
🎯 Goal: Build a PowerShell script that uses a switch statement with wildcard and regex patterns to classify file names into categories.
📋 What You'll Learn
Create a list of file names in a variable called files with exact values.
Create a variable called specialPattern to hold a regex pattern.
Use a switch statement with wildcard and regex matching to categorize each file name.
Print the file name and its category.
💡 Why This Matters
🌍 Real World
Automating file management tasks by categorizing files based on their names helps organize data and trigger specific actions.
💼 Career
Knowing how to use switch with wildcard and regex in PowerShell is useful for system administrators and automation engineers managing files and logs.
Progress0 / 4 steps
1
Create the list of file names
Create a variable called files and assign it an array with these exact file names: report1.txt, data_backup.zip, image01.png, notes.docx, temp123.tmp.
PowerShell
Need a hint?

Use @( ... ) to create an array in PowerShell.

2
Create the regex pattern variable
Create a variable called specialPattern and assign it the regex pattern string ^temp\d+\.tmp$ to match file names starting with 'temp' followed by digits and ending with '.tmp'.
PowerShell
Need a hint?

Remember to escape backslashes in the regex string.

3
Use switch with wildcard and regex
Use a switch statement with -Wildcard and -Regex parameters to check each file in $files. Use these cases:
- Wildcard *.txt prints ' is a text file'.
- Wildcard *.zip prints ' is a zip archive'.
- Regex matching $specialPattern prints ' is a special temp file'.
- Default case prints ' is an unknown file type'.
Use foreach to loop over $files and apply the switch inside the loop.
PowerShell
Need a hint?

Use nested switch statements to handle wildcard and regex separately.

4
Print the categorized file types
Run the script to print the category of each file as per the switch logic. The output should show each file name followed by its category message.
PowerShell
Need a hint?

Run the script and check the console output matches the expected categories.