Bird
0
0

What will be the output of this PowerShell command?

medium📝 Command Output Q4 of 15
PowerShell - Variables and Data Types
What will be the output of this PowerShell command?
2..4 | ForEach-Object { "Index: $_" }
AIndex: 1 Index: 2 Index: 3
BIndex: 2 Index: 3 Index: 4
CIndex: $_ Index: $_ Index: $_
D2..4
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range operator

    The expression 2..4 generates the sequence 2, 3, 4.
  2. Step 2: Role of $_ in ForEach-Object

    Inside the script block, $_ represents the current number from the pipeline.
  3. Step 3: Output construction

    Each iteration outputs a string "Index: " followed by the current number.
  4. Final Answer:

    Index: 2 Index: 3 Index: 4 -> Option B
  5. Quick Check:

    Range 2 to 4 piped, $_ outputs each number [OK]
Quick Trick: Range operator outputs sequence; $_ is current item [OK]
Common Mistakes:
  • Assuming $_ starts at 1 instead of 2
  • Thinking output is literal $_
  • Confusing range operator with string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes