Challenge - 5 Problems
PowerShell Automatic Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this PowerShell pipeline?
Consider the following PowerShell command:
What is the output?
1..3 | ForEach-Object { $_ * 2 }What is the output?
PowerShell
1..3 | ForEach-Object { $_ * 2 }
Attempts:
2 left
💡 Hint
$_ represents the current item in the pipeline inside ForEach-Object.
✗ Incorrect
In PowerShell, $_ is the automatic variable for the current pipeline object. Multiplying each number 1, 2, 3 by 2 results in 2, 4, 6.
💻 Command Output
intermediate1:30remaining
What does $PSVersionTable.PSVersion.Major return?
If you run this command in PowerShell:
What kind of value will it return?
$PSVersionTable.PSVersion.Major
What kind of value will it return?
PowerShell
$PSVersionTable.PSVersion.Major
Attempts:
2 left
💡 Hint
$PSVersionTable contains version info as properties.
✗ Incorrect
$PSVersionTable is an automatic variable holding version info. PSVersion is a version object, and Major is its major version number as an integer.
📝 Syntax
advanced2:00remaining
Which option correctly uses $_ in a Where-Object filter?
You want to filter numbers greater than 5 from a list using Where-Object. Which of these commands is correct?
Attempts:
2 left
💡 Hint
Use the correct comparison operator and variable name.
✗ Incorrect
Option A uses $_ (current object) and the correct PowerShell comparison operator '-gt' for 'greater than'. Option A uses '>' which is invalid in PowerShell filters.
🔧 Debug
advanced2:00remaining
Why does this script fail to filter correctly?
Given this script:
It outputs 1 3 5 but you want only even numbers. What is wrong?
1..5 | ForEach-Object { if ($_ % 2) { $_ } }It outputs 1 3 5 but you want only even numbers. What is wrong?
PowerShell
1..5 | ForEach-Object { if ($_ % 2) { $_ } }
Attempts:
2 left
💡 Hint
Remember how modulo (%) works with odd and even numbers.
✗ Incorrect
The expression '$_ % 2' returns 1 for odd numbers (true) and 0 for even (false). So the if condition passes for odd numbers, outputting them. To get evens, check for '$_ % 2 -eq 0'.
🚀 Application
expert2:30remaining
How to display PowerShell version in a formatted string?
You want to print the PowerShell version as:
using $PSVersionTable. Which command produces this exact output?
PowerShell version: X.Y.Z
using $PSVersionTable. Which command produces this exact output?
Attempts:
2 left
💡 Hint
Use subexpressions $() to embed properties inside strings.
✗ Incorrect
Option B correctly uses subexpressions $() to insert each version part inside the string. Option B treats properties as literal text. Option B uses $_ which is undefined here. Option B uses invalid syntax for property access.