Bird
Raised Fist0
PowerShellscripting~10 mins

Automatic variables ($_, $PSVersionTable) in PowerShell - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Automatic variables ($_, $PSVersionTable)
Start Script
↓
Use $_ in pipeline
↓
$_ holds current item
↓
Process item
↓
Repeat for each item
↓
Use $PSVersionTable
↓
Display PowerShell version info
↓
End Script
The script processes each item in a pipeline using $_ as the current item, then accesses $PSVersionTable to show PowerShell version details.
Execution Sample
PowerShell
1..3 | ForEach-Object { "Number: $_" }
$PSVersionTable.PSVersion
This code prints each number from 1 to 3 with a label, then shows the PowerShell version.
Execution Table
StepPipeline Item ($_)ActionOutput
11Print "Number: 1"Number: 1
22Print "Number: 2"Number: 2
33Print "Number: 3"Number: 3
4N/AAccess $PSVersionTable.PSVersionMajor.Minor.Build.Revision (e.g., 7.3.4.0)
5N/AEnd of scriptNo more output
💡 All pipeline items processed; script ends after displaying PowerShell version.
Variable Tracker
VariableStartAfter 1After 2After 3Final
$_N/A123N/A
$PSVersionTable.PSVersionN/AN/AN/AN/A7.3.4.0
Key Moments - 2 Insights
Why does $_ change inside the ForEach-Object loop?
Because $_ automatically holds the current item from the pipeline during each loop iteration, as shown in execution_table steps 1 to 3.
What is $PSVersionTable and why is it accessed after the loop?
$PSVersionTable is an automatic variable holding PowerShell version info; it is accessed after the loop to display version details, as in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $_ at step 2?
A1
B3
C2
DN/A
💡 Hint
Check the 'Pipeline Item ($_)' column at step 2 in the execution_table.
At which step does the script display the PowerShell version?
AStep 3
BStep 4
CStep 5
DStep 1
💡 Hint
Look for the action 'Access $PSVersionTable.PSVersion' in the execution_table.
If the pipeline was 1..2 instead of 1..3, how many times would $_ change?
A2 times
B3 times
C1 time
D0 times
💡 Hint
Refer to variable_tracker for $_ changes per iteration.
Concept Snapshot
Automatic variables in PowerShell:
- $_ holds the current pipeline item inside loops.
- Use $_ to refer to each item during ForEach-Object.
- $PSVersionTable stores PowerShell version info.
- Access $PSVersionTable.PSVersion to see version details.
- $_ changes each iteration; $PSVersionTable is static.
Full Transcript
This example shows how PowerShell uses automatic variables. The variable $_ holds the current item in a pipeline during a ForEach-Object loop. For each number from 1 to 3, the script prints 'Number: ' followed by the current number stored in $_. After processing all items, the script accesses $PSVersionTable.PSVersion to display the PowerShell version. The execution table traces each step, showing $_ changing from 1 to 3, and then the version info being displayed. The variable tracker confirms $_ updates each iteration, while $PSVersionTable remains constant. Key moments clarify why $_ changes and what $PSVersionTable represents. The quiz tests understanding of $_ values at specific steps and when the version info is shown.

Practice

(1/5)
1. What does the automatic variable $_ represent in a PowerShell pipeline?
easy
A. The version of PowerShell installed
B. The current object being processed in the pipeline
C. The last error message generated
D. The total number of commands run

Solution

  1. Step 1: Understand the role of $_ in pipelines

    $_ is a special variable that holds the current item being processed in a pipeline command.
  2. Step 2: Differentiate from other automatic variables

    $PSVersionTable shows version info, errors use $Error, and command counts are not stored in $_.
  3. Final Answer:

    The current object being processed in the pipeline -> Option B
  4. Quick Check:

    $_ = current pipeline item [OK]
Hint: Remember: $_ always means current pipeline item [OK]
Common Mistakes:
  • Confusing $_ with $PSVersionTable
  • Thinking $_ holds error info
  • Assuming $_ is a global variable
2. Which of the following is the correct way to display the PowerShell version using $PSVersionTable?
easy
A. Write-Output $PSVersionTable.VersionInfo
B. Write-Output $_.Version
C. Write-Output $PSVersion.VersionTable
D. Write-Output $PSVersionTable.Version

Solution

  1. Step 1: Identify the correct property of $PSVersionTable

    The property Version holds the PowerShell version number.
  2. Step 2: Confirm the syntax to output the version

    Write-Output $PSVersionTable.Version correctly prints the version; other options use wrong variable names or properties.
  3. Final Answer:

    Write-Output $PSVersionTable.Version -> Option D
  4. Quick Check:

    $PSVersionTable.Version outputs version [OK]
Hint: Use $PSVersionTable.Version to get PowerShell version [OK]
Common Mistakes:
  • Using $_ instead of $PSVersionTable
  • Swapping property names incorrectly
  • Assuming $PSVersionTable is a method
3. What will be the output of this PowerShell command?
1..3 | ForEach-Object { "Number: $_" }
medium
A. Number: 1 Number: 2 Number: 3
B. Number: $_ Number: $_ Number: $_
C. 1 2 3
D. Error: $_ is undefined

Solution

  1. Step 1: Understand the pipeline and $_ usage

    The pipeline sends numbers 1 to 3 one by one to ForEach-Object. Inside the script block, $_ holds the current number.
  2. Step 2: Evaluate the output string for each number

    Each iteration outputs "Number: 1", then "Number: 2", then "Number: 3" on separate lines.
  3. Final Answer:

    Number: 1 Number: 2 Number: 3 -> Option A
  4. Quick Check:

    Pipeline item $_ prints each number [OK]
Hint: $_ holds current pipeline item inside ForEach-Object [OK]
Common Mistakes:
  • Thinking $_ is a literal string
  • Expecting all output on one line without spaces
  • Confusing $_ with a variable outside pipeline
4. Identify the error in this script snippet:
Get-Process | Where-Object { $_.Name -eq 'powershell' } | ForEach-Object { Write-Output $PSVersionTable }
medium
A. The script outputs the entire $PSVersionTable multiple times unnecessarily
B. Using $PSVersionTable inside ForEach-Object is invalid
C. $_ is not defined inside Where-Object script block
D. Get-Process cannot be piped to Where-Object

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]
Hint: 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
5. You want to list all running processes and display their names along with your PowerShell version once at the end. Which script correctly uses $_ and $PSVersionTable to achieve this?
hard
A. Get-Process | ForEach-Object { Write-Output "Process: $PSVersionTable.Version" }; Write-Output "PowerShell Version: $_.Name"
B. Get-Process | ForEach-Object { Write-Output "Process: $_.Name; PowerShell Version: $PSVersionTable.Version" }
C. Get-Process | ForEach-Object { Write-Output "Process: $_.Name" }; Write-Output "PowerShell Version: $PSVersionTable.Version"
D. Write-Output "PowerShell Version: $PSVersionTable.Version"; Get-Process | ForEach-Object { Write-Output "Process: $_.Name" }

Solution

  1. Step 1: Understand the requirement to show processes and version separately

    We want to list each process name using $_ inside the pipeline, then show PowerShell version once after.
  2. Step 2: Evaluate each option's correctness

    Get-Process | ForEach-Object { Write-Output "Process: $_.Name" }; Write-Output "PowerShell Version: $PSVersionTable.Version" lists processes with $_ correctly, then outputs version once after the loop. Get-Process | ForEach-Object { Write-Output "Process: $_.Name; PowerShell Version: $PSVersionTable.Version" } repeats version for each process. Write-Output "PowerShell Version: $PSVersionTable.Version"; Get-Process | ForEach-Object { Write-Output "Process: $_.Name" } outputs version first, then processes (order reversed). Get-Process | ForEach-Object { Write-Output "Process: $PSVersionTable.Version" }; Write-Output "PowerShell Version: $_.Name" misuses variables inside the loop.
  3. Final Answer:

    Get-Process | ForEach-Object { Write-Output "Process: $_.Name" }; Write-Output "PowerShell Version: $PSVersionTable.Version" -> Option C
  4. Quick Check:

    List processes with $_, version once after [OK]
Hint: Output static info after pipeline, use $_ inside pipeline [OK]
Common Mistakes:
  • Repeating version output inside loop
  • Mixing order of outputs
  • Using wrong variable names inside pipeline
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard