Bird
0
0

Which of the following is the correct syntax to double each number from 1 to 3 using ForEach-Object?

easy📝 Syntax Q3 of 15
PowerShell - Cmdlets and Pipeline
Which of the following is the correct syntax to double each number from 1 to 3 using ForEach-Object?
A1..3 | ForEach-Object $_ * 2
B1..3 | ForEach-Object { $_ * 2 }
C1..3 ForEach-Object { $_ * 2 }
DForEach-Object 1..3 { $_ * 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand pipeline usage

    In PowerShell, the range 1..3 is piped into ForEach-Object using |.
  2. Step 2: Check script block syntax

    The script block must be enclosed in braces { } and use $_ to refer to the current item.
  3. Final Answer:

    1..3 | ForEach-Object { $_ * 2 } -> Option B
  4. Quick Check:

    Correct syntax uses pipe and script block with $_ [OK]
Quick Trick: Pipe input and use { $_ } for current item in ForEach-Object [OK]
Common Mistakes:
  • Omitting the pipe symbol
  • Not using braces for the script block
  • Using ForEach-Object without pipeline

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes