0
0
PowerShellscripting~10 mins

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

Choose your learning style9 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.