Introduction
PowerShell versions 5.1 and 7+ are different tools to run commands and scripts on Windows and other systems. Knowing the difference helps you pick the right one for your tasks.
To check your PowerShell version, run: $PSVersionTable.PSVersion
$PSVersionTable.PSVersion
pwsh
Get-Host | Select-Object Version
# Check PowerShell version Write-Output "Your PowerShell version is: $($PSVersionTable.PSVersion)" # Example: Use a feature only in PowerShell 7+ if ($PSVersionTable.PSVersion.Major -ge 7) { Write-Output "You can use the new 'ForEach-Object -Parallel' feature!" } else { Write-Output "You are using Windows PowerShell 5.1 or older." }
$PSVersionTable.PSVersion.Major
if ($PSVersionTable.PSVersion.Major -ge 7) { Write-Output "New version" } else { Write-Output "Old version" }if ($PSVersionTable.PSVersion.Major -ge 7) { # run feature } -> Option CPass the test and this pattern moves unknown