0
0
PowerShellscripting~20 mins

ForEach loop in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell ForEach Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a ForEach loop with array modification
What is the output of this PowerShell script?
PowerShell
$arr = 1,2,3,4
foreach ($i in $arr) {
  $i = $i * 2
  Write-Output $i
}
Write-Output $arr
A
1
2
3
4
1 2 3 4
B
2
4
6
8
2 4 6 8
C
2
4
6
8
1 2 3 4
D
2 4 6 8
2 4 6 8
Attempts:
2 left
💡 Hint
Remember that modifying the loop variable inside a ForEach does not change the original array elements.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this ForEach loop
Which option contains a syntax error in the ForEach loop?
PowerShell
foreach ($item in $list) {
  Write-Output $item
}
Aforeach $item in $list { Write-Output $item }
Bforeach ($item in $list) { Write-Output $item }
C} meti$ tuptuO-etirW { )tsil$ ni meti$( hcaerof
Dforeach ($item in $list) Write-Output $item
Attempts:
2 left
💡 Hint
Check the parentheses around the loop variable.
🔧 Debug
advanced
2:00remaining
Why does this ForEach loop not output anything?
Given this script, why is there no output?
PowerShell
$numbers = 1..5
foreach ($num in $numbers) {
  $num * 2
}
ABecause the array $numbers is empty.
BBecause the multiplication operator * is invalid in PowerShell.
CBecause the foreach loop syntax is incorrect.
DBecause the loop variable $num is not being output explicitly.
Attempts:
2 left
💡 Hint
In PowerShell, expressions inside loops need to be output explicitly to display.
🚀 Application
advanced
2:30remaining
Using ForEach to modify a list of objects
You have a list of objects with a property 'Name'. You want to append '-done' to each Name. Which script correctly updates the objects?
PowerShell
$items = @(
  [PSCustomObject]@{Name='Task1'},
  [PSCustomObject]@{Name='Task2'}
)
foreach ($item in $items) {
  # update Name here
}
Aforeach ($item in $items) { $item.Name = $item.Name + '-done' }
Bforeach ($item in $items) { $item = $item.Name + '-done' }
Cforeach ($item in $items) { $item.Name = $item.Name - '-done' }
Dforeach ($item in $items) { $item.Name += '-done' }
Attempts:
2 left
💡 Hint
Remember to assign the new string back to the property.
🧠 Conceptual
expert
3:00remaining
Understanding pipeline ForEach-Object vs ForEach loop
Which statement about PowerShell's ForEach-Object cmdlet and ForEach loop is TRUE?
AForEach loop can only process arrays, but ForEach-Object can process any collection.
BForEach-Object processes items one at a time as they come through the pipeline, while ForEach loop processes all items after the collection is fully available.
CForEach-Object is faster than ForEach loop because it uses parallel processing by default.
DForEach loop can be used in pipelines directly, but ForEach-Object cannot.
Attempts:
2 left
💡 Hint
Think about how pipeline processing works in PowerShell.