Bird
Raised Fist0
PowerShellscripting~20 mins

ForEach loop in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the foreach loop do in PowerShell?
easy
A. Creates a new collection from scratch
B. Runs code only once regardless of the collection
C. Repeats a block of code for each item in a collection
D. Stops the script immediately

Solution

  1. Step 1: Understand the foreach loop purpose

    The foreach loop is designed to repeat actions for every item in a list or collection.
  2. Step 2: Compare options to this purpose

    Only Repeats a block of code for each item in a collection correctly describes repeating code for each item in a collection.
  3. Final Answer:

    Repeats a block of code for each item in a collection -> Option C
  4. Quick Check:

    foreach = repeat for each item [OK]
Hint: Think: foreach means 'for each item' repeat [OK]
Common Mistakes:
  • Confusing foreach with a one-time action
  • Thinking foreach creates new collections
  • Believing foreach stops the script
2. Which of the following is the correct syntax for a foreach loop in PowerShell?
easy
A. foreach ($item in $list) { Write-Output $item }
B. foreach $item in $list { Write-Output $item }
C. foreach ($item : $list) { Write-Output $item }
D. foreach ($item; $list) { Write-Output $item }

Solution

  1. Step 1: Recall correct foreach syntax

    PowerShell foreach loops use: foreach ($var in $collection) { ... }
  2. Step 2: Match options to syntax

    Only foreach ($item in $list) { Write-Output $item } uses the correct 'in' keyword and parentheses.
  3. Final Answer:

    foreach ($item in $list) { Write-Output $item } -> Option A
  4. Quick Check:

    Syntax uses 'in' keyword inside parentheses [OK]
Hint: Look for 'foreach ($var in $collection)' pattern [OK]
Common Mistakes:
  • Omitting parentheses around variable and collection
  • Using ':' or ';' instead of 'in'
  • Missing the 'in' keyword
3. What will be the output of this PowerShell script?
foreach ($num in 1..3) { Write-Output ($num * 2) }
medium
A. 2 4 6
B. 1 2 3
C. 3 6 9
D. Error: Invalid syntax

Solution

  1. Step 1: Understand the loop range

    The loop runs for numbers 1, 2, and 3.
  2. Step 2: Calculate output for each iteration

    Each number is multiplied by 2, so outputs are 2, 4, and 6.
  3. Final Answer:

    2 4 6 -> Option A
  4. Quick Check:

    1*2=2, 2*2=4, 3*2=6 [OK]
Hint: Multiply each loop number by 2 mentally [OK]
Common Mistakes:
  • Confusing the range operator '..'
  • Thinking output is original numbers
  • Expecting syntax error
4. Identify the error in this PowerShell foreach loop:
foreach $item in $array { Write-Output $item }
medium
A. No error, code is correct
B. Missing parentheses around the loop variable and collection
C. Using $item instead of $i as loop variable
D. Incorrect use of Write-Output

Solution

  1. Step 1: Check foreach syntax

    PowerShell requires parentheses around the variable and collection in foreach loops.
  2. Step 2: Identify missing parentheses

    The code lacks parentheses: it should be foreach ($item in $array) { ... }
  3. Final Answer:

    Missing parentheses around the loop variable and collection -> Option B
  4. Quick Check:

    foreach needs parentheses [OK]
Hint: Always use parentheses in foreach syntax [OK]
Common Mistakes:
  • Omitting parentheses
  • Confusing variable names
  • Thinking Write-Output causes error
5. You have a list of filenames stored in $files. You want to print only those filenames that end with '.txt' using a foreach loop. Which script correctly does this?
hard
A. foreach ($file in $files) { if ($file -eq '*.txt') { Write-Output $file } }
B. foreach ($file in $files) { if ($file contains '.txt') { Write-Output $file } }
C. foreach ($file in $files) { Write-Output $file -match '.txt' }
D. foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } }

Solution

  1. Step 1: Understand filtering condition

    To check if a filename ends with '.txt', use the '-like' operator with '*.txt' pattern.
  2. Step 2: Verify foreach loop and condition

    foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } } correctly uses foreach with an if condition checking '-like "*.txt"' and outputs matching files.
  3. Final Answer:

    foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } } -> Option D
  4. Quick Check:

    Use '-like' with wildcard for pattern match [OK]
Hint: Use '-like "*.txt"' inside if in foreach [OK]
Common Mistakes:
  • Using '-eq' instead of '-like' for pattern matching
  • Misusing '-match' without if condition
  • Using 'contains' which is invalid here
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard