Automatic variables in PowerShell hold special information that helps scripts run smoothly without extra setup.
Automatic variables ($_, $PSVersionTable) in PowerShell
Introduction
Syntax
PowerShell
$_ $PSVersionTable
$_ represents the current object in the pipeline.
$PSVersionTable is a read-only table showing PowerShell version details.
Examples
$_ to refer to each process.PowerShell
Get-Process | Where-Object { $_.CPU -gt 100 }PowerShell
Write-Output $PSVersionTable.PSVersion
$_ inside a loop.PowerShell
Get-Service | ForEach-Object { "Service: $_.Name" }Sample Program
This script first shows PowerShell version info, then lists all running services by using $PSVersionTable and $_.
PowerShell
Write-Output "PowerShell version info:" Write-Output $PSVersionTable "List of running services with status:" | Write-Output Get-Service | Where-Object { $_.Status -eq 'Running' } | ForEach-Object { Write-Output "Service: $_.Name" }
Important Notes
$_ only works inside commands that process pipeline input like ForEach-Object or Where-Object.
$PSVersionTable is useful to check compatibility before running scripts.
Summary
$_ is the current item in a pipeline, making it easy to work with each piece of data.
$PSVersionTable shows details about your PowerShell version and environment.
These automatic variables help write shorter, clearer scripts without extra setup.
Practice
1. What does the automatic variable
$_ represent in a PowerShell pipeline?easy
Solution
Step 1: Understand the role of
$_in pipelines$_is a special variable that holds the current item being processed in a pipeline command.Step 2: Differentiate from other automatic variables
$PSVersionTableshows version info, errors use$Error, and command counts are not stored in$_.Final Answer:
The current object being processed in the pipeline -> Option BQuick 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
Solution
Step 1: Identify the correct property of
The property$PSVersionTableVersionholds the PowerShell version number.Step 2: Confirm the syntax to output the version
Write-Output $PSVersionTable.Versioncorrectly prints the version; other options use wrong variable names or properties.Final Answer:
Write-Output $PSVersionTable.Version -> Option DQuick Check:
$PSVersionTable.Versionoutputs 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
Solution
Step 1: Understand the pipeline and
The pipeline sends numbers 1 to 3 one by one to$_usageForEach-Object. Inside the script block,$_holds the current number.Step 2: Evaluate the output string for each number
Each iteration outputs "Number: 1", then "Number: 2", then "Number: 3" on separate lines.Final Answer:
Number: 1 Number: 2 Number: 3 -> Option AQuick 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
Solution
Step 1: Analyze the pipeline and variable usage
The pipeline filters processes named 'powershell' and then outputs$PSVersionTablefor each matching process.Step 2: Identify inefficiency or error
$PSVersionTableis a static variable; outputting it inside the loop repeats the same info multiple times unnecessarily.Final Answer:
The script outputs the entire $PSVersionTable multiple times unnecessarily -> Option AQuick 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
Solution
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.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.Final Answer:
Get-Process | ForEach-Object { Write-Output "Process: $_.Name" }; Write-Output "PowerShell Version: $PSVersionTable.Version" -> Option CQuick 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
10 questions ~5 mineasy hard
