What if one script could run perfectly on any computer, old or new, without changes?
PowerShell versions (5.1 vs 7+) - When to Use Which
Imagine you need to run scripts on different computers, some with older Windows systems and others with newer setups. You try to use the same commands everywhere, but they don't always work as expected.
Using only the old PowerShell 5.1 means missing out on faster performance and new features. Scripts can break on newer systems or when using modern tools. Manually checking each system and rewriting scripts wastes time and causes frustration.
PowerShell 7+ is a modern, cross-platform version that works on Windows, Mac, and Linux. It supports new commands and runs scripts faster. Knowing the differences helps you write scripts that work everywhere without headaches.
if ($PSVersionTable.PSVersion.Major -eq 5) { Write-Host 'Old PowerShell' }
if ($PSVersionTable.PSVersion.Major -ge 7) { Write-Host 'Modern PowerShell' }
You can create powerful, reliable scripts that run smoothly on any system, saving time and avoiding errors.
A system admin automates software installs on both old Windows 10 machines and new Linux servers using one script that adapts to the PowerShell version automatically.
Old PowerShell (5.1) is limited to Windows and fewer features.
PowerShell 7+ is faster, cross-platform, and supports modern scripting.
Knowing versions helps write scripts that work everywhere without extra work.
Practice
Solution
Step 1: Understand PowerShell 5.1 platform support
PowerShell 5.1 is built for Windows and comes pre-installed on many Windows machines.Step 2: Compare with PowerShell 7+
PowerShell 7+ is newer, faster, and supports Windows, Mac, and Linux.Final Answer:
PowerShell 5.1 is Windows-only and comes pre-installed on many Windows PCs. -> Option BQuick Check:
PowerShell 5.1 = Windows-only [OK]
- Thinking PowerShell 5.1 works on Mac/Linux
- Confusing which version is newer
- Assuming PowerShell 7+ is Windows-only
Solution
Step 1: Recall the built-in variable for version info
$PSVersionTable is a built-in variable that holds version details.Step 2: Access the PSVersion property
Using $PSVersionTable.PSVersion returns the current PowerShell version object.Final Answer:
$PSVersionTable.PSVersion -> Option AQuick Check:
Check version = $PSVersionTable.PSVersion [OK]
- Using non-existent commands like Get-PSVersion
- Confusing variable names
- Trying commands that don't exist in PowerShell
$PSVersionTable.PSVersion.Major
Solution
Step 1: Understand the command
$PSVersionTable.PSVersion returns a version object; .Major accesses the major version number.Step 2: Identify the major version in PowerShell 7+
PowerShell 7+ means the major version is 7, so the output is 7.Final Answer:
7 -> Option DQuick Check:
Major version in PS 7+ = 7 [OK]
- Assuming major version is 5 for PowerShell 7+
- Expecting full version string instead of major number
- Thinking .Major property does not exist
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?
Solution
Step 1: Analyze the condition logic
The script checks if major version is 7 or greater to print "New version".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.Final Answer:
The script is running in PowerShell 5.1, not 7+. -> Option AQuick Check:
Output "Old version" means PS version < 7 [OK]
- Thinking -ge operator is invalid in 5.1
- Assuming $PSVersionTable.PSVersion is missing in 5.1
- Believing Write-Output behaves differently in 7+
Solution
Step 1: Identify correct property for major version
$PSVersionTable.PSVersion.Major gives the major version number (5 or 7).Step 2: Use proper comparison operator
Using -ge 7 checks if version is 7 or higher, suitable for feature gating.Final Answer:
Useif ($PSVersionTable.PSVersion.Major -ge 7) { # run feature }-> Option CQuick Check:
Check major version with .Major and -ge 7 [OK]
- Comparing full version object instead of .Major
- Using .Minor instead of .Major for major version check
- Using wrong property like $PSVersionTable.Version
Ready to prove it?
Pass the test and this pattern moves unknown
