Check-PSVersion

Q: How to be sure the script will run with the installed version of Powershell?
A: Add a function to check the PowerShell host version at the begining of the script, and ‘break’ if it’s not the right version…

In this example, we will check that the PowerShell v3 is installed.

function Check-PSVersion {
$PowerShellVersion=$host.version.major
if (3 -eq $PowerShellVersion) {Write-Host "PowerShell v${PowerShellVersion} is OK to run this script." -ForegroundColor DarkGreen}
else
{Write-Host "PowerShell v${PowerShellVersion} not compatible with this script. Requires version 3" -ForegroundColor DarkRed ; break}
} # EndOf function CheckPSVersion

Leave a Reply to Fab Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top