0
0
PowerShellscripting~20 mins

Automatic variables ($_, $PSVersionTable) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Automatic Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this PowerShell pipeline?
Consider the following PowerShell command:
1..3 | ForEach-Object { $_ * 2 }

What is the output?
PowerShell
1..3 | ForEach-Object { $_ * 2 }
A1 2 3
BError: $_ is undefined
C0 2 4
D2 4 6
Attempts:
2 left
💡 Hint
$_ represents the current item in the pipeline inside ForEach-Object.
💻 Command Output
intermediate
1:30remaining
What does $PSVersionTable.PSVersion.Major return?
If you run this command in PowerShell:
$PSVersionTable.PSVersion.Major

What kind of value will it return?
PowerShell
$PSVersionTable.PSVersion.Major
AA boolean indicating if PowerShell is installed
BA string with the full PowerShell version
CAn integer representing the major version number of PowerShell
DAn error because $PSVersionTable is undefined
Attempts:
2 left
💡 Hint
$PSVersionTable contains version info as properties.
📝 Syntax
advanced
2: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?
A1..10 | Where-Object { $_ -gt 5 }
B1..10 | Where-Object { $ -gt 5 }
C1..10 | Where-Object { $_ > 5 }
D1..10 | Where-Object { $_ -lt 5 }
Attempts:
2 left
💡 Hint
Use the correct comparison operator and variable name.
🔧 Debug
advanced
2:00remaining
Why does this script fail to filter correctly?
Given this script:
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) { $_ } }
AThe condition '$_ % 2' is true for odd numbers, so it outputs odds instead of evens
B$_ is not defined inside ForEach-Object, causing an error
CThe script is missing a closing brace '}'
DThe pipeline operator '|' is used incorrectly
Attempts:
2 left
💡 Hint
Remember how modulo (%) works with odd and even numbers.
🚀 Application
expert
2:30remaining
How to display PowerShell version in a formatted string?
You want to print the PowerShell version as:
PowerShell version: X.Y.Z

using $PSVersionTable. Which command produces this exact output?
AWrite-Output "PowerShell version: $PSVersionTable.PSVersion.Major.$PSVersionTable.PSVersion.Minor.$PSVersionTable.PSVersion.Build"
BWrite-Output "PowerShell version: $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor).$($PSVersionTable.PSVersion.Build)"
CWrite-Output "PowerShell version: $_.PSVersion.Major.$_.PSVersion.Minor.$_.PSVersion.Build"
DWrite-Output "PowerShell version: $PSVersionTable[PSVersion][Major].[Minor].[Build]"
Attempts:
2 left
💡 Hint
Use subexpressions $() to embed properties inside strings.