0
0
PowerShellscripting~10 mins

ForEach-Object for iteration 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 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'
A-ScriptBlock
B-Process
C-FilterScript
D-InputObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using -InputObject instead of -Process causes errors.
Trying to use -FilterScript which is not a valid parameter.
2fill in blank
medium

Complete 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'
A+
B/
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using + adds 2 instead of multiplying.
Using / divides the number.
3fill in blank
hard

Fix 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'
AUpperCase
Btoupper
CToUpper
DtoUpperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase method names causes errors.
Using JavaScript style method names like toUpperCase.
4fill in blank
hard

Fill 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'
A>
B<
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using < filters numbers less than 3, which is wrong here.
Using + adds numbers instead of squaring.
5fill in blank
hard

Fill 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'
A-gt
B$_
CLength
Dlength
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.