Discover how PowerShell's automatic helpers can do the thinking for you and save hours of tedious work!
Why Automatic variables ($_, $PSVersionTable) in PowerShell? - Purpose & Use Cases
Imagine you need to process a list of files one by one and check your PowerShell version manually every time you run a script. You open the console, type commands repeatedly, and try to remember the current item you are working on.
This manual way is slow and tiring. You might forget which file you just processed or which version of PowerShell you are using. It's easy to make mistakes or waste time typing the same info again and again.
Automatic variables like $_ and $PSVersionTable help by holding useful info for you. $_ always points to the current item in a loop, so you don't have to track it yourself. $PSVersionTable shows your PowerShell version details instantly, so you can adapt your script easily.
foreach ($item in $list) { Write-Host $item; } Write-Host "PowerShell version: 5.1"
$list | ForEach-Object { Write-Host $_ }
Write-Host $PSVersionTable.PSVersionWith these automatic variables, your scripts become smarter and faster, handling data and environment info without extra effort.
When cleaning up files, $_ lets you act on each file automatically, and $PSVersionTable helps you check if your script needs to change for different PowerShell versions.
Automatic variables save you from tracking details manually.
$_ represents the current item in loops or pipelines.
$PSVersionTable gives quick access to your PowerShell version info.
Practice
$_ represent in a PowerShell pipeline?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]
- Confusing $_ with $PSVersionTable
- Thinking $_ holds error info
- Assuming $_ is a global variable
$PSVersionTable?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]
- Using $_ instead of $PSVersionTable
- Swapping property names incorrectly
- Assuming $PSVersionTable is a method
1..3 | ForEach-Object { "Number: $_" }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]
- Thinking $_ is a literal string
- Expecting all output on one line without spaces
- Confusing $_ with a variable outside pipeline
Get-Process | Where-Object { $_.Name -eq 'powershell' } | ForEach-Object { Write-Output $PSVersionTable }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]
- Thinking $PSVersionTable is undefined in pipeline
- Believing $_ is invalid in Where-Object
- Assuming Get-Process can't be piped
$_ and $PSVersionTable to achieve this?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]
- Repeating version output inside loop
- Mixing order of outputs
- Using wrong variable names inside pipeline
Ready to prove it?
Pass the test and this pattern moves unknown
