Avatar of IT CAMPER
IT CAMPER
Flag for United States of America asked on

PowerShell script to check version and prompt if older than required to continue

I am starting to run more and more PS scripts and often run into some servers running outdated versions of PS that need to be updated before the scripts will run. I'd like to add a PS version check at the top of every script (I only have a few actually) that checks the installed version and then compares that against the minimum version required. If the installed version is too old then it prompts the user with the versions and information indicating the issue. If the installed version is newer then it continues with the script.

Powershell

Avatar of undefined
Last Comment
footech

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rainer Jeschor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
footech

How about just using the #Requires statement? Like
#Requires -Version 5.1

Open in new window

If the running version isn't equal to or above the stated version you get an error like below.
The script 'somescript.ps1' cannot be run because it contained a "#requires" statement for Windows
PowerShell 5.2. The version of Windows PowerShell that is required by the script does not match the currently running
version of Windows PowerShell 5.1.18362.1801.

Open in new window


Run help about_requires for more info.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck