0
0
PowerShellscripting~10 mins

match operator in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the string contains the word 'hello' using the match operator.

PowerShell
$text = 'hello world'
if ($text [1] 'hello') {
    Write-Output 'Match found'
}
Drag options to blanks, or click blank then click option'
A-eq
B-match
C-like
D-contains
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-eq' which checks for exact equality, not pattern matching.
Using '-like' which uses wildcard matching but is different from '-match'.
2fill in blank
medium

Complete the code to find all words starting with 'a' in the array using the match operator.

PowerShell
$words = @('apple', 'banana', 'apricot', 'cherry')
$result = $words | Where-Object { $_ [1] '^a' }
Write-Output $result
Drag options to blanks, or click blank then click option'
A-match
B-eq
C-like
D-contains
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which uses wildcards but not regex.
Using '-eq' which checks exact match.
3fill in blank
hard

Fix the error in the code to correctly check if the variable contains digits using the match operator.

PowerShell
$input = 'abc123'
if ($input [1] '\d+') {
    Write-Output 'Digits found'
}
Drag options to blanks, or click blank then click option'
A-like
B-contains
C-match
D-eq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which does not support regex.
Using '-contains' which is for collections, not strings.
4fill in blank
hard

Fill both blanks to filter an array for strings that end with '.txt' using the match operator.

PowerShell
$files = @('doc1.txt', 'image.png', 'notes.txt', 'script.ps1')
$txtFiles = $files | Where-Object { $_ [1] [2] }
Write-Output $txtFiles
Drag options to blanks, or click blank then click option'
A-match
B'\.txt$'
C'*.txt'
D-like
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' with '*.txt' which works but is not the match operator.
Using '-match' with '*.txt' which is not a regex pattern.
5fill in blank
hard

Fill all three blanks to create a hashtable of words and their lengths only if the word contains the letter 'e' using the match operator.

PowerShell
$words = @('tree', 'sky', 'bee', 'cloud')
$lengths = @{ }
foreach ($word in $words) {
    if ($word [1] [2]) {
        $lengths[[3]] = $word.Length
    }
}
Write-Output $lengths
Drag options to blanks, or click blank then click option'
A-match
B'e'
C$word
D-like
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' instead of '-match' for regex matching.
Using a string literal instead of the variable as the hashtable key.