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 {
if (3 -eq $PowerShellVersion) {Write-Host "PowerShell v${PowerShellVersion} is OK to run this script." -ForegroundColor DarkGreen}
{Write-Host "PowerShell v${PowerShellVersion} not compatible with this script. Requires version 3" -ForegroundColor DarkRed ; break}
} # EndOf function CheckPSVersion

Leave a Reply

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

Scroll to Top