0
0
PowerShellscripting~10 mins

String comparison (-like, -match) 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 variable $name contains the letter 'a' using the -like operator.

PowerShell
$name = "Sarah"
if ($name [1] "*a*") {
    Write-Output "Contains 'a'"
}
Drag options to blanks, or click blank then click option'
A-contains
B-like
C-eq
D-match
Attempts:
3 left
💡 Hint
Common Mistakes
Using -eq which checks exact equality.
Using -contains which is for collections, not strings.
2fill in blank
medium

Complete the code to check if the variable $text matches the regex pattern for digits using the -match operator.

PowerShell
$text = "Order123"
if ($text [1] '\d+') {
    Write-Output "Contains digits"
}
Drag options to blanks, or click blank then click option'
A-match
B-like
C-eq
D-contains
Attempts:
3 left
💡 Hint
Common Mistakes
Using -like which does not support regex.
Using -eq which checks exact equality.
3fill in blank
hard

Fix the error in the code to correctly check if $filename ends with '.txt' using -like.

PowerShell
$filename = "report.doc"
if ($filename [1] "*.txt") {
    Write-Output "Text file"
} else {
    Write-Output "Not a text file"
}
Drag options to blanks, or click blank then click option'
A-contains
B-eq
C-match
D-like
Attempts:
3 left
💡 Hint
Common Mistakes
Using -eq which requires exact match.
Using -match which expects regex, but pattern is wildcard.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths, but only include words longer than 4 characters.

PowerShell
$words = @('apple', 'cat', 'banana', 'dog')
$lengths = $words | ForEach-Object { $word = $_; @{ [1] = [2] } } | Where-Object { $_.Value -gt 4 }
Drag options to blanks, or click blank then click option'
A'word'
B'length'
C$word
D$word.Length
Attempts:
3 left
💡 Hint
Common Mistakes
Using string literals instead of variables for keys or values.
Not using the Length property to get word length.
5fill in blank
hard

Fill all three blanks to create a hashtable of uppercase words and their lengths, including only words with length greater than 3.

PowerShell
$words = @('tree', 'sun', 'flower', 'sky')
$result = $words | ForEach-Object { $word = $_; @{ [1] = [2] } } | Where-Object { [3] }
Drag options to blanks, or click blank then click option'
A$word.ToUpper()
B$word.Length
C$word.Length -gt 3
D$word
Attempts:
3 left
💡 Hint
Common Mistakes
Using original words instead of uppercase for keys.
Not filtering correctly by length.