Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print each number in the list using ForEach-Object.
PowerShell
1, 2, 3, 4, 5 | ForEach-Object [1] { Write-Output $_ }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -InputObject instead of -Process causes errors.
Trying to use -FilterScript which is not a valid parameter.
✗ Incorrect
The -Process parameter is used with ForEach-Object to specify the script block to run for each item.
2fill in blank
mediumComplete the code to multiply each number by 2 and output the result.
PowerShell
1, 2, 3 | ForEach-Object -Process { $_ [1] 2 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using + adds 2 instead of multiplying.
Using / divides the number.
✗ Incorrect
The * operator multiplies each number by 2.
3fill in blank
hardFix the error in the code to correctly output each item in uppercase.
PowerShell
'apple', 'banana', 'cherry' | ForEach-Object -Process { $_.[1]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase method names causes errors.
Using JavaScript style method names like toUpperCase.
✗ Incorrect
The correct method name in PowerShell to convert a string to uppercase is ToUpper().
4fill in blank
hardFill both blanks to filter numbers greater than 3 and square them.
PowerShell
1, 2, 3, 4, 5 | Where-Object { $_ [1] 3 } | ForEach-Object -Process { $_ [2] $_ }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < filters numbers less than 3, which is wrong here.
Using + adds numbers instead of squaring.
✗ Incorrect
Use > to filter numbers greater than 3, and * to square each number.
5fill in blank
hardFill all three blanks to create a hashtable $h of words and their lengths for words longer than 4 letters.
PowerShell
$h = @{}; 'apple', 'bat', 'carrot', 'dog' | ForEach-Object { if ( $_.Length [1] 4 ) { $h[[2]] = $_.[3] } } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt filters short words instead.
Using a literal instead of $_ for the key.
Using lowercase 'length' which does not match the Length property.
✗ Incorrect
Use -gt to check greater than 4, $_ as the key (the word), and Length as the property for the value (length).