Bird
Raised Fist0
PowerShellscripting~20 mins

ForEach-Object for iteration in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ForEach-Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of ForEach-Object with simple arithmetic
What is the output of this PowerShell command?
1..5 | ForEach-Object { $_ * 2 }
PowerShell
1..5 | ForEach-Object { $_ * 2 }
A1 2 3 4 5
B2 4 6 8 10
C0 2 4 6 8
DError: Unexpected token
Attempts:
2 left
💡 Hint
Remember $_ represents the current item in the pipeline.
💻 Command Output
intermediate
2:00remaining
ForEach-Object with conditional output
What does this PowerShell command output?
1..4 | ForEach-Object { if ($_ % 2 -eq 0) { $_ } }
PowerShell
1..4 | ForEach-Object { if ($_ % 2 -eq 0) { $_ } }
A1 2 3 4
B1 3
C2 4
DError: Missing else block
Attempts:
2 left
💡 Hint
Only even numbers are output because of the condition.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in ForEach-Object usage
Which option contains a syntax error in using ForEach-Object?
A1..3 | ForEach-Object Write-Output $_
B1..3 | ForEach-Object { $_ * 3 }
C1..3 | ForEach-Object { Write-Output $_ }
D1..3 | ForEach-Object { $_ + 1 }
Attempts:
2 left
💡 Hint
ForEach-Object requires a script block enclosed in braces {}.
🔧 Debug
advanced
2:00remaining
Why does this ForEach-Object command output only 4 and 5?
Given this command:
1..5 | ForEach-Object { if ($_ -gt 3) { $_ } }

Why does it output only 4 and 5, but not 1, 2, 3?
PowerShell
1..5 | ForEach-Object { if ($_ -gt 3) { $_ } }
ABecause the if condition filters out numbers not greater than 3
BBecause ForEach-Object only processes numbers greater than 3 by default
CBecause the pipeline stops after first two numbers
DBecause $_ is not defined inside the script block
Attempts:
2 left
💡 Hint
Check the condition inside the if statement.
🚀 Application
expert
3:00remaining
Using ForEach-Object to modify and output a list
You have a list of filenames: 'file1.txt', 'file2.txt', 'file3.txt'.
Which command outputs the filenames without the '.txt' extension?
A@('file1.txt','file2.txt','file3.txt') | ForEach-Object { $_.Remove('.txt') }
B@('file1.txt','file2.txt','file3.txt') | ForEach-Object { $_.Replace('.txt','') }
C@('file1.txt','file2.txt','file3.txt') | ForEach-Object { $_ -replace '.txt', '' }
D@('file1.txt','file2.txt','file3.txt') | ForEach-Object { $_ -replace '\.txt$', '' }
Attempts:
2 left
💡 Hint
Use regex to remove only the '.txt' at the end of the string.

Practice

(1/5)
1. What does the ForEach-Object cmdlet do in PowerShell?
easy
A. Runs a block of code on each item in a collection
B. Creates a new object from a template
C. Deletes items from a list
D. Sorts items in a collection

Solution

  1. Step 1: Understand the purpose of ForEach-Object

    ForEach-Object is designed to process each item in a collection by running a script block on it.
  2. Step 2: Compare with other options

    Options A, B, and C describe different cmdlets or actions unrelated to iteration over items.
  3. Final Answer:

    Runs a block of code on each item in a collection -> Option A
  4. Quick Check:

    ForEach-Object = runs code on each item [OK]
Hint: Remember: ForEach-Object processes items one by one [OK]
Common Mistakes:
  • Confusing ForEach-Object with sorting or filtering cmdlets
  • Thinking it creates or deletes objects
  • Assuming it works only on arrays, not collections
2. Which of the following is the correct syntax to use ForEach-Object to print each item in a list?
easy
A. 1..3 | ForEach-Object { Write-Output $_ }
B. ForEach-Object 1..3 { Write-Output $_ }
C. Write-Output 1..3 | ForEach-Object $_
D. 1..3 ForEach-Object { $_ }

Solution

  1. Step 1: Identify correct pipeline usage

    In PowerShell, ForEach-Object is used after a pipeline to process each item, so the syntax is: collection | ForEach-Object { script }
  2. Step 2: Check the script block and variable usage

    The script block uses $_ to refer to the current item. 1..3 | ForEach-Object { Write-Output $_ } correctly uses this syntax.
  3. Final Answer:

    1..3 | ForEach-Object { Write-Output $_ } -> Option A
  4. Quick Check:

    Pipeline + ForEach-Object + { $_ } = correct syntax [OK]
Hint: Use pipeline before ForEach-Object and $_ inside braces [OK]
Common Mistakes:
  • Placing ForEach-Object before the collection
  • Not using curly braces for the script block
  • Using $_ outside the script block
3. What is the output of this PowerShell command?
1..3 | ForEach-Object { $_ * 2 }
medium
A. 3 6 9
B. 1 2 3
C. 2 4 6
D. Error: Cannot multiply

Solution

  1. Step 1: Understand the input and operation

    The input is numbers 1, 2, 3. ForEach-Object multiplies each by 2.
  2. Step 2: Calculate each iteration

    1*2=2, 2*2=4, 3*2=6, so output is 2 4 6.
  3. Final Answer:

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

    Each item * 2 = 2 4 6 [OK]
Hint: Multiply each item inside ForEach-Object with $_ [OK]
Common Mistakes:
  • Assuming output is original numbers
  • Confusing multiplication with addition
  • Expecting an error due to syntax
4. Identify the error in this script:
1..3 | ForEach-Object { Write-Output $item }
medium
A. ForEach-Object cannot process number ranges
B. Missing pipeline operator before ForEach-Object
C. Incorrect use of Write-Output cmdlet
D. Variable $item is undefined inside the script block

Solution

  1. Step 1: Check variable usage inside ForEach-Object

    Inside ForEach-Object, the current item is referenced by $_, not $item.
  2. Step 2: Verify other syntax elements

    The pipeline and Write-Output usage are correct, and ForEach-Object can process number ranges.
  3. Final Answer:

    Variable $item is undefined inside the script block -> Option D
  4. Quick Check:

    Use $_ for current item, not $item [OK]
Hint: Use $_ inside ForEach-Object, not custom variable names [OK]
Common Mistakes:
  • Using undefined variables instead of $_
  • Forgetting the pipeline operator
  • Misunderstanding ForEach-Object capabilities
5. You have a list of file names: @('file1.txt', 'file2.txt.log', 'file3.txt'). Using ForEach-Object, how do you print only the names ending with '.txt'?
hard
A. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { $_.EndsWith('.txt') }
B. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_ -like '*.txt') { Write-Output $_ } }
C. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { Write-Output $_ -like '*.txt' }
D. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_.Contains('.txt')) { $_ } }

Solution

  1. Step 1: Understand filtering inside ForEach-Object

    To print only items ending with '.txt', use an if condition with -like '*.txt' inside the script block.
  2. Step 2: Check each option's correctness

    @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_ -like '*.txt') { Write-Output $_ } } correctly uses if with -like and Write-Output. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { Write-Output $_ -like '*.txt' } uses invalid syntax (Write-Output does not accept -like as parameter). @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { $_.EndsWith('.txt') } outputs boolean values instead of filtered names. @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_.Contains('.txt')) { $_ } } uses Contains which matches anywhere, not just the end (prints all three).
  3. Final Answer:

    @('file1.txt', 'file2.txt.log', 'file3.txt') | ForEach-Object { if ($_ -like '*.txt') { Write-Output $_ } } -> Option B
  4. Quick Check:

    Use if + -like '*.txt' inside ForEach-Object [OK]
Hint: Use if ($_ -like '*.txt') inside ForEach-Object to filter [OK]
Common Mistakes:
  • Outputting boolean results instead of filtered items
  • Using string methods incorrectly in PowerShell
  • Using Contains instead of EndsWith or -like for suffix
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