📖 Scenario: You work in IT support and often need to check if user inputs match common patterns like email addresses, phone numbers, or postal codes.
🎯 Goal: Build a PowerShell script that uses common regex patterns to find matches in given text samples.
📋 What You'll Learn
Create variables with sample text strings
Define regex pattern variables for email, phone number, and postal code
Use the -match operator with the regex patterns to check for matches
Print the results clearly showing which pattern matched
💡 Why This Matters
🌍 Real World
IT support and automation scripts often need to validate user input like emails or phone numbers to ensure data quality.
💼 Career
Knowing common regex patterns and how to apply them in scripts is useful for system administrators, support engineers, and automation specialists.
Progress0 / 4 steps
1
Create sample text variables
Create three variables called $emailSample, $phoneSample, and $postalSample with these exact string values: 'contact@example.com', '123-456-7890', and '90210' respectively.
PowerShell
Hint
Use = to assign the exact strings to the variables.
2
Define regex pattern variables
Create three variables called $emailPattern, $phonePattern, and $postalPattern with these exact regex strings: '^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$' for email, '^\d{3}-\d{3}-\d{4}$' for phone number, and '^\d{5}$' for postal code.
PowerShell
Hint
Use single quotes and escape backslashes properly in regex strings.
3
Check matches using regex patterns
Use the -match operator with variables $emailSample, $phoneSample, and $postalSample against $emailPattern, $phonePattern, and $postalPattern respectively. Store the boolean results in variables $emailMatch, $phoneMatch, and $postalMatch.
PowerShell
Hint
Use -match to test if the sample matches the pattern and assign the result to the match variables.
4
Print the match results
Print the values of $emailMatch, $phoneMatch, and $postalMatch each on a new line using Write-Output.
PowerShell
Hint
Use Write-Output to print each match result on its own line.
Practice
(1/5)
1. Which regex pattern matches any single digit from 0 to 9 in PowerShell?
easy
A. \d
B. \w
C. \s
D. \D
Solution
Step 1: Understand regex digit shorthand
The pattern \d matches any single digit character (0-9) in regex.
C. Using single quotes prevents regex from working
D. Regex pattern is correct, no error
Solution
Step 1: Review regex pattern components
The pattern matches username with allowed chars, an @, domain name, a literal dot (escaped), and domain extension of 2+ letters.
Step 2: Check PowerShell string usage
Single quotes in PowerShell treat string literally, so \ is treated as a single backslash, correctly escaping the dot.
Final Answer:
Regex pattern is correct, no error -> Option D
Quick Check:
Proper escaping and pattern = Correct [OK]
Hint: Single quotes keep regex escapes intact in PowerShell [OK]
Common Mistakes:
Thinking dot needs double escape in single quotes
Assuming single quotes disable regex
Misreading character classes as incorrect
5. You want to extract all words starting with a capital letter from a string in PowerShell. Which regex pattern should you use?
hard
A. \b[a-z][A-Z]*\b
B. \b[A-Z][a-z]*\b
C. [A-Z]+\b
D. \b[A-Z]+[0-9]*\b
Solution
Step 1: Understand the requirement
We want words starting with a capital letter, followed by zero or more lowercase letters.
Step 2: Analyze each pattern
A: \b[A-Z][a-z]*\b matches word boundary, one uppercase letter, then zero or more lowercase letters, then word boundary - correct. B: starts with lowercase letter, so incorrect. C: matches one or more uppercase letters but no word boundary start. D: matches uppercase letters followed by digits, not only letters.
Final Answer:
\b[A-Z][a-z]*\b -> Option B
Quick Check:
Capital start + lowercase letters = \b[A-Z][a-z]*\b [OK]
Hint: Use \b for word boundary and [A-Z] for capital start [OK]
Common Mistakes:
Ignoring word boundaries causing partial matches
Using [a-z][A-Z]* which starts with lowercase
Including digits when only letters needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.