What does the <code>ForEach-Object</code> cmdlet do in PowerShell?
It processes each item in a collection one at a time, allowing you to run a script block on each item.
Click to reveal answer
beginner
How do you access the current item inside the ForEach-Object script block?
Use the automatic variable $_ to refer to the current object being processed.
Click to reveal answer
beginner
Example: What will this command output?
1..3 | ForEach-Object { $_ * 2 }
It outputs: 2 4 6 Each number from 1 to 3 is multiplied by 2.
Click to reveal answer
intermediate
Can ForEach-Object be used to perform actions other than outputting values? Give an example.
Yes, it can run any code for each item, like writing to a file or calling a function. Example:
1..3 | ForEach-Object { Write-Host "Number: $_" }
Click to reveal answer
intermediate
What is the difference between ForEach-Object and the foreach keyword in PowerShell?
ForEach-Object works with pipeline input and processes items one by one as they come. foreach is a loop that works on a collection already in memory. Use ForEach-Object for streaming data, foreach for looping over arrays.
Click to reveal answer
What variable holds the current item inside a ForEach-Object script block?
A$item
B$_
C$this
D$current
✗ Incorrect
The automatic variable $_ always refers to the current object in the pipeline.
Which of these commands doubles each number from 1 to 5 using ForEach-Object?
AForEach-Object 1..5 { $_ * 2 }
BForEach-Object { 1..5 * 2 }
C1..5 | ForEach-Object { $_ * 2 }
D1..5 | ForEach-Object { 2 }
✗ Incorrect
Option C correctly pipes numbers 1 to 5 and multiplies each by 2 inside the script block.
What is the main use of ForEach-Object in PowerShell?
ATo create functions
BTo declare variables
CTo stop script execution
DTo process each item in a pipeline one by one
✗ Incorrect
ForEach-Object is designed to handle each pipeline item individually.
Which statement is true about ForEach-Object?
AIt processes items as they come through the pipeline
BIt only works with arrays stored in variables
CIt cannot modify items
DIt is used to define functions
✗ Incorrect
ForEach-Object works on streaming pipeline input, processing items one at a time.
How would you print each item from a list using ForEach-Object?
A1..3 | ForEach-Object { Write-Host $_ }
BWrite-Host 1..3
CForEach-Object { Write-Host $_ } 1..3
DWrite-Host ForEach-Object 1..3
✗ Incorrect
Option A correctly pipes numbers 1 to 3 and prints each using Write-Host inside the script block.
Explain how ForEach-Object works in PowerShell and how you use it to process items.
Think about how you handle each item one by one as it flows through the pipeline.
You got /4 concepts.
Describe the difference between ForEach-Object and the foreach loop in PowerShell.
One works with pipeline data, the other with collections already loaded.
You got /3 concepts.
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
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.
Step 2: Compare with other options
Options A, B, and C describe different cmdlets or actions unrelated to iteration over items.
Final Answer:
Runs a block of code on each item in a collection -> Option A
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
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 }
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.
Final Answer:
1..3 | ForEach-Object { Write-Output $_ } -> Option A
Inside ForEach-Object, the current item is referenced by $_, not $item.
Step 2: Verify other syntax elements
The pipeline and Write-Output usage are correct, and ForEach-Object can process number ranges.
Final Answer:
Variable $item is undefined inside the script block -> Option D
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 $_ } }