What is the purpose of the automatic variable $_ in PowerShell?
The $_ variable represents the current object in the pipeline. It is used inside loops and commands like ForEach-Object to refer to each item being processed.
Click to reveal answer
beginner
What information does the $PSVersionTable automatic variable provide?
$PSVersionTable contains details about the PowerShell environment, including the version number, edition, and platform information.
Click to reveal answer
intermediate
How would you use $_ to filter files larger than 1MB in a directory?
You can use Get-ChildItem and Where-Object with $_.Length to filter files: Get-ChildItem -File | Where-Object { $_.Length -gt 1MB }
Click to reveal answer
intermediate
Explain why $PSVersionTable is useful when writing scripts.
It helps scripts check the PowerShell version and environment to ensure compatibility or enable version-specific features.
Click to reveal answer
beginner
In a ForEach-Object loop, what does $_ represent and why is it important?
$_ represents the current item being processed in the loop. It is important because it allows you to access properties or perform actions on each item individually.
Click to reveal answer
What does the automatic variable $_ represent in PowerShell?
AThe PowerShell version information
BThe current user's username
CThe last error message
DThe current object in the pipeline
✗ Incorrect
The $_ variable always holds the current object being processed in the pipeline or loop.
Which automatic variable contains PowerShell version and environment details?
A$PSVersionTable
B$_
C$Error
D$Host
✗ Incorrect
$PSVersionTable holds detailed information about the PowerShell version and environment.
How would you access the size of a file in a pipeline using $_?
A$_.Length
B$_.Path
C$_.Size
D$_.Name
✗ Incorrect
The Length property of a file object gives its size in bytes, accessed via $_.Length.
Why might a script check $PSVersionTable before running?
ATo get the current user's permissions
BTo verify the current directory
CTo confirm the PowerShell version supports needed features
DTo list running processes
✗ Incorrect
Scripts check $PSVersionTable to ensure the PowerShell version supports required commands or syntax.
In which scenario is $_ most commonly used?
ATo store error messages
BInside a ForEach-Object loop
CTo display PowerShell version
DTo set environment variables
✗ Incorrect
$_ is commonly used inside ForEach-Object or Where-Object to represent the current pipeline item.
Describe how the automatic variable $_ works in a PowerShell pipeline and give an example of its use.
Think about how you process each item one by one in a list.
You got /3 concepts.
Explain what information $PSVersionTable provides and why it is useful in scripting.
Consider why a script might want to know which PowerShell version is running.
You got /3 concepts.
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?