Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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'.
✗ Incorrect
The '-match' operator checks if the string on the left contains the pattern on the right.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which uses wildcards but not regex.
Using '-eq' which checks exact match.
✗ Incorrect
The '-match' operator supports regular expressions, so '^a' matches words starting with 'a'.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-like' which does not support regex.
Using '-contains' which is for collections, not strings.
✗ Incorrect
The '-match' operator uses regex patterns like '\d+' to find digits in a string.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Use '-match' with the regex '\.txt$' to find strings ending with '.txt'.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use '-match' with pattern 'e' to check if the word contains 'e', then use the word as the key in the hashtable.