0
0
PowerShellscripting~20 mins

Regular expressions with -match in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Regular Expressions with -match in PowerShell
📖 Scenario: You work in a small company and need to filter a list of email addresses to find only those from a specific domain.
🎯 Goal: Build a PowerShell script that uses the -match operator with a regular expression to find email addresses ending with @example.com.
📋 What You'll Learn
Create a list of email addresses in a variable called emails.
Create a regular expression pattern variable called pattern to match emails ending with @example.com.
Use a foreach loop to check each email with -match and collect matching emails in a list called matchedEmails.
Print the list of matched emails.
💡 Why This Matters
🌍 Real World
Filtering email lists is common in marketing, IT, and customer support to target or exclude specific groups.
💼 Career
Knowing how to use regular expressions with PowerShell helps automate text processing tasks in system administration and data management roles.
Progress0 / 4 steps
1
Create the list of email addresses
Create a variable called emails and assign it an array with these exact email addresses: "alice@example.com", "bob@test.com", "carol@example.com", "dave@work.com".
PowerShell
Need a hint?

Use @() to create an array in PowerShell.

2
Create the regular expression pattern
Create a variable called pattern and assign it the string '@example\.com$' to match emails ending with @example.com.
PowerShell
Need a hint?

Use \. to match a literal dot and $ to match the end of the string.

3
Filter emails using -match
Create an empty array called matchedEmails. Use a foreach loop with variable email to go through $emails. Inside the loop, use if ($email -match $pattern) to check if the email matches the pattern. If it matches, add $email to matchedEmails.
PowerShell
Need a hint?

Use += to add items to an array in PowerShell.

4
Print the matched emails
Use Write-Output to print the $matchedEmails array.
PowerShell
Need a hint?

Use Write-Output to show the results in PowerShell.