Explore PowerShell Automatic Variables $_ and $PSVersionTable
📖 Scenario: You are working with PowerShell to process a list of files and want to learn how to use automatic variables to make your scripts simpler and more powerful.
🎯 Goal: Learn to use the automatic variable $_ inside a loop to access the current item, and explore the $PSVersionTable automatic variable to check your PowerShell version.
📋 What You'll Learn
Create an array of file names
Use the automatic variable $_ inside a loop to print each file name
Access the $PSVersionTable automatic variable to display PowerShell version info
Print the results clearly
💡 Why This Matters
🌍 Real World
PowerShell scripts often process lists of files or data items. Using automatic variables like $_ makes it easy to write concise and readable scripts that handle each item automatically.
💼 Career
Understanding automatic variables is essential for system administrators and automation engineers who write PowerShell scripts to manage systems, automate tasks, and gather system information efficiently.
Progress0 / 4 steps
1
Create an array of file names
Create a variable called $files and assign it an array with these exact file names: 'report.docx', 'data.csv', 'image.png', 'notes.txt'.
PowerShell
Hint
Use @( ... ) to create an array in PowerShell.
2
Set up a loop to process each file
Add a foreach loop that goes through each item in $files using the automatic variable $_ inside the loop body.
PowerShell
Hint
Use ForEach-Object { ... } and inside the braces, $_ is the current item.
3
Print each file name using $_
Inside the ForEach-Object loop, add a Write-Output command to print the current file name stored in $_.
PowerShell
Hint
Use Write-Output $_ to print the current item.
4
Display PowerShell version using $PSVersionTable
Add a line to print the PowerShell version information stored in the automatic variable $PSVersionTable.
PowerShell
Hint
Use Write-Output $PSVersionTable to show version details.
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
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
$PSVersionTable shows version info, errors use $Error, and command counts are not stored in $_.
Final Answer:
The current object being processed in the pipeline -> Option B
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
Step 1: Identify the correct property of $PSVersionTable
The property Version holds the PowerShell version number.
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.
Final Answer:
Write-Output $PSVersionTable.Version -> Option D
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
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.
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 A
Quick Check:
Pipeline item $_ prints each number [OK]
Hint: $_ holds current pipeline item inside ForEach-Object [OK]
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?