0
0
PowerShellscripting~15 mins

match operator in PowerShell - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the match operator in PowerShell
📖 Scenario: You work in a small office where you receive messages from different departments. Each message starts with a keyword like 'Sales', 'HR', or 'IT'. You want to quickly check the type of message and print a response.
🎯 Goal: Create a PowerShell script that uses the match operator to check the message type and print a matching response.
📋 What You'll Learn
Create a variable $message with a specific message string
Create a variable $keyword to hold the first word of the message
Use the match operator to compare $keyword with known departments
Print the correct response based on the matched keyword
💡 Why This Matters
🌍 Real World
This script helps quickly identify message types in office communication for faster processing.
💼 Career
Using the match operator is useful for filtering logs, processing text, and automating responses in IT and administrative jobs.
Progress0 / 4 steps
1
Create the message variable
Create a variable called $message and set it to the string 'Sales: Please send the monthly report.'
PowerShell
Need a hint?

Use = to assign the string to $message.

2
Extract the keyword
Create a variable called $keyword and set it to the first word before the colon in $message using the -split operator.
PowerShell
Need a hint?

Use -split ':' to split the string and take the first part with [0].

3
Use the match operator
Use the match operator to compare $keyword with the strings 'Sales', 'HR', and 'IT'. Assign the result to a variable called $result. Use a single line with the syntax: $keyword -match 'pattern' where 'pattern' matches any of the three keywords.
PowerShell
Need a hint?

Use the pipe | to separate multiple options in the pattern.

4
Print the result
Print 'Match found' if $result is $true, otherwise print 'No match'. Use an if statement.
PowerShell
Need a hint?

Use if ($result) { Write-Output 'Match found' } else { Write-Output 'No match' }.