Bird
0
0

Identify the error in this script snippet:

medium📝 Debug Q14 of 15
PowerShell - Variables and Data Types
Identify the error in this script snippet:
Get-Process | Where-Object { $_.Name -eq 'powershell' } | ForEach-Object { Write-Output $PSVersionTable }
AThe script outputs the entire $PSVersionTable multiple times unnecessarily
BUsing $PSVersionTable inside ForEach-Object is invalid
C$_ is not defined inside Where-Object script block
DGet-Process cannot be piped to Where-Object
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the pipeline and variable usage

    The pipeline filters processes named 'powershell' and then outputs $PSVersionTable for each matching process.
  2. Step 2: Identify inefficiency or error

    $PSVersionTable is a static variable; outputting it inside the loop repeats the same info multiple times unnecessarily.
  3. Final Answer:

    The script outputs the entire $PSVersionTable multiple times unnecessarily -> Option A
  4. Quick Check:

    Repeated $PSVersionTable output inside loop [OK]
Quick Trick: Avoid repeating static variables inside loops [OK]
Common Mistakes:
  • Thinking $PSVersionTable is undefined in pipeline
  • Believing $_ is invalid in Where-Object
  • Assuming Get-Process can't be piped

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes