Bird
Raised Fist0
PowerShellscripting~20 mins

PowerShell versions (5.1 vs 7+) - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Version Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
PowerShell Version Output
What is the output of this command in PowerShell 7+?

$PSVersionTable.PSVersion.Major
PowerShell
$PSVersionTable.PSVersion.Major
A7
B5
C6
DError: Property not found
Attempts:
2 left
💡 Hint
Check the major version number property in PowerShell 7+.
📝 Syntax
intermediate
2:00remaining
Using Ternary Operator in PowerShell 7+
Which option correctly uses the ternary operator introduced in PowerShell 7+ to assign a value to $result based on $x?
PowerShell
$x = 10
$result = <fill_here>
A$result = if ($x -gt 5) { 'Greater' } else { 'Smaller' }
B$result = $x -gt 5 ? 'Greater' : 'Smaller'
C$result = $x > 5 ? 'Greater' : 'Smaller'
D$result = $x -gt 5 then 'Greater' else 'Smaller'
Attempts:
2 left
💡 Hint
PowerShell 7+ supports the ternary operator with ? : syntax and uses -gt for greater than.
🔧 Debug
advanced
2:00remaining
Why does this script fail in PowerShell 5.1 but work in 7+?
Given this script:
1..5 | ForEach-Object { $_.ToString() -replace '^', 'Num-' }

Why does it fail in PowerShell 5.1 but work in 7+?
ABecause the -replace operator does not exist in PowerShell 5.1
BBecause in PowerShell 5.1, the pipeline variable $_ is not available inside script blocks
CBecause in PowerShell 5.1, the ToString() method returns a different type that cannot be used with -replace
DBecause in PowerShell 5.1, the -replace operator does not support regex anchors like '^'
Attempts:
2 left
💡 Hint
Check how ToString() output interacts with -replace in different versions.
🧠 Conceptual
advanced
1:30remaining
Cross-platform Compatibility in PowerShell Versions
Which statement best describes the cross-platform support differences between PowerShell 5.1 and PowerShell 7+?
APowerShell 5.1 is Windows-only, while PowerShell 7+ supports Windows, Linux, and macOS natively.
BBoth PowerShell 5.1 and 7+ support Windows and Linux, but only 7+ supports macOS.
CPowerShell 5.1 runs natively on Windows, Linux, and macOS, while PowerShell 7+ only runs on Windows.
DPowerShell 5.1 and 7+ both support only Windows natively; cross-platform support requires extra tools.
Attempts:
2 left
💡 Hint
Consider which PowerShell version introduced cross-platform support.
🚀 Application
expert
2:30remaining
Detecting PowerShell Version in a Script
You want a script that prints 'Legacy PowerShell' if running on version 5.1 or lower, and 'Modern PowerShell' if running on 7 or higher. Which script produces the correct output?
Aif ($PSVersionTable.PSVersion.Major -lt 7) { 'Modern PowerShell' } else { 'Legacy PowerShell' }
Bif ($PSVersionTable.PSVersion.Major -le 5) { 'Legacy PowerShell' } else { 'Modern PowerShell' }
Cif ($PSVersionTable.PSVersion.Major -gt 5) { 'Legacy PowerShell' } else { 'Modern PowerShell' }
Dif ($PSVersionTable.PSVersion.Major -ge 7) { 'Modern PowerShell' } else { 'Legacy PowerShell' }
Attempts:
2 left
💡 Hint
Check the comparison operators and logic for version numbers.

Practice

(1/5)
1. Which statement correctly describes PowerShell 5.1 compared to PowerShell 7+?
easy
A. PowerShell 7+ is only available on Linux systems.
B. PowerShell 5.1 is Windows-only and comes pre-installed on many Windows PCs.
C. PowerShell 5.1 is cross-platform and faster than PowerShell 7+.
D. PowerShell 7+ is the older version and no longer supported.

Solution

  1. Step 1: Understand PowerShell 5.1 platform support

    PowerShell 5.1 is built for Windows and comes pre-installed on many Windows machines.
  2. Step 2: Compare with PowerShell 7+

    PowerShell 7+ is newer, faster, and supports Windows, Mac, and Linux.
  3. Final Answer:

    PowerShell 5.1 is Windows-only and comes pre-installed on many Windows PCs. -> Option B
  4. Quick Check:

    PowerShell 5.1 = Windows-only [OK]
Hint: Remember: 5.1 = Windows-only, 7+ = cross-platform [OK]
Common Mistakes:
  • Thinking PowerShell 5.1 works on Mac/Linux
  • Confusing which version is newer
  • Assuming PowerShell 7+ is Windows-only
2. Which of the following is the correct PowerShell command to check your PowerShell version?
easy
A. $PSVersionTable.PSVersion
B. Get-PSVersion
C. Check-PSVersion
D. Show-Version

Solution

  1. Step 1: Recall the built-in variable for version info

    $PSVersionTable is a built-in variable that holds version details.
  2. Step 2: Access the PSVersion property

    Using $PSVersionTable.PSVersion returns the current PowerShell version object.
  3. Final Answer:

    $PSVersionTable.PSVersion -> Option A
  4. Quick Check:

    Check version = $PSVersionTable.PSVersion [OK]
Hint: Use $PSVersionTable.PSVersion to see your PowerShell version [OK]
Common Mistakes:
  • Using non-existent commands like Get-PSVersion
  • Confusing variable names
  • Trying commands that don't exist in PowerShell
3. What will be the output of this command in PowerShell 7+?
$PSVersionTable.PSVersion.Major
medium
A. 5
B. Error: Property not found
C. 1
D. 7

Solution

  1. Step 1: Understand the command

    $PSVersionTable.PSVersion returns a version object; .Major accesses the major version number.
  2. Step 2: Identify the major version in PowerShell 7+

    PowerShell 7+ means the major version is 7, so the output is 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    Major version in PS 7+ = 7 [OK]
Hint: Major version number is the first digit in $PSVersionTable.PSVersion [OK]
Common Mistakes:
  • Assuming major version is 5 for PowerShell 7+
  • Expecting full version string instead of major number
  • Thinking .Major property does not exist
4. You run this script in PowerShell 5.1:
if ($PSVersionTable.PSVersion.Major -ge 7) { Write-Output "New version" } else { Write-Output "Old version" }

But it always outputs "Old version" even on PowerShell 7+. What is the likely problem?
medium
A. The script is running in PowerShell 5.1, not 7+.
B. The comparison operator -ge is invalid in PowerShell 5.1.
C. $PSVersionTable.PSVersion does not exist in PowerShell 5.1.
D. Write-Output cannot print strings in PowerShell 7+.

Solution

  1. Step 1: Analyze the condition logic

    The script checks if major version is 7 or greater to print "New version".
  2. Step 2: Understand why it outputs "Old version"

    If it always prints "Old version", the major version is less than 7, meaning the script runs in PowerShell 5.1 or older.
  3. Final Answer:

    The script is running in PowerShell 5.1, not 7+. -> Option A
  4. Quick Check:

    Output "Old version" means PS version < 7 [OK]
Hint: If condition fails, check which PowerShell version runs the script [OK]
Common Mistakes:
  • Thinking -ge operator is invalid in 5.1
  • Assuming $PSVersionTable.PSVersion is missing in 5.1
  • Believing Write-Output behaves differently in 7+
5. You want to write a script that uses a feature only available in PowerShell 7+, but your script might run on 5.1. How should you check the version before running the feature?
hard
A. Use if ($PSVersionTable.PSVersion -eq 7) { # run feature }
B. Use if ($PSVersionTable.Version -gt 7) { # run feature }
C. Use if ($PSVersionTable.PSVersion.Major -ge 7) { # run feature }
D. Use if ($PSVersionTable.PSVersion.Minor -ge 7) { # run feature }

Solution

  1. Step 1: Identify correct property for major version

    $PSVersionTable.PSVersion.Major gives the major version number (5 or 7).
  2. Step 2: Use proper comparison operator

    Using -ge 7 checks if version is 7 or higher, suitable for feature gating.
  3. Final Answer:

    Use if ($PSVersionTable.PSVersion.Major -ge 7) { # run feature } -> Option C
  4. Quick Check:

    Check major version with .Major and -ge 7 [OK]
Hint: Check major version with .Major and -ge 7 before running features [OK]
Common Mistakes:
  • Comparing full version object instead of .Major
  • Using .Minor instead of .Major for major version check
  • Using wrong property like $PSVersionTable.Version
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard