📖 Scenario: You work in an IT team that manages many Windows computers. You need to check which PowerShell version is installed on each computer to decide if you can use new features from PowerShell 7 or if you must stick to PowerShell 5.1.
🎯 Goal: Build a simple PowerShell script that shows the installed PowerShell version and tells if it is version 5.1 or older, or version 7 or newer.
📋 What You'll Learn
Create a variable to hold the PowerShell version information.
Create a variable to hold the version threshold number 7.
Use an if statement to check if the installed version is 7 or higher.
Print a message showing the installed version and whether it is 5.1 or older, or 7 or newer.
💡 Why This Matters
🌍 Real World
IT professionals often need to know which PowerShell version is installed to use the right commands and scripts safely.
💼 Career
Knowing how to check and compare software versions is a basic skill for system administrators and automation engineers.
Progress0 / 4 steps
1
Get PowerShell version information
Create a variable called psVersion and set it to the value of $PSVersionTable.PSVersion.Major to get the major PowerShell version number.
PowerShell
Hint
Use $PSVersionTable.PSVersion.Major to get the main version number of PowerShell.
2
Set version threshold
Create a variable called versionThreshold and set it to 7 to represent the minimum version for PowerShell 7 or newer.
PowerShell
Hint
Just assign the number 7 to a variable named versionThreshold.
3
Check PowerShell version
Write an if statement that checks if $psVersion is greater than or equal to $versionThreshold. Inside the if, create a variable called message and set it to "PowerShell version $psVersion is 7 or newer.". In the else block, set message to "PowerShell version $psVersion is 5.1 or older.".
PowerShell
Hint
Use -ge to compare numbers in PowerShell. Remember to use double quotes for strings with variables.
4
Display the result
Write a Write-Output command to print the message variable.
PowerShell
Hint
Use Write-Output $message to show the message on screen.
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
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 B
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
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 A
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
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 D
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
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 A
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
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:
Use if ($PSVersionTable.PSVersion.Major -ge 7) { # run feature } -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.