Recall & Review
beginner
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?<br>
1..3 | ForEach-Object { $_ * 2 }It outputs:<br>2<br>4<br>6<br>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.<br>Example:<br>
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.<br>foreach is a loop that works on a collection already in memory.<br>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?✗ 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?✗ 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?✗ Incorrect
ForEach-Object is designed to handle each pipeline item individually.Which statement is true about
ForEach-Object?✗ 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?✗ 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.