Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check which Version my PS Script needs?

Tags:

powershell

Sometimes when I'm writing a script on my Windows 10 Machine (PowerShell 5.0) I use commands, parameters or aliases which are not available on earlier versions of PowerShell, e.g the -persist parameter of new-psdrive is not available on PowerShell 2.0 which all of our Win7 machines use.

to set my #requires -version x statement correctly, I need to know if there are commands in my script which aren't available to earlier PowerShell Versions. When you wrote a code with 1000 lines it could be a little difficult to find unavailable commands in your script.

Is there a way to check this programmatically, other than just run the script in different PowerShell environments and see what's happening?

like image 914
SimonS Avatar asked Nov 09 '22 15:11

SimonS


1 Answers

Have you considered developing on your Windows 10 machine but set your powershell profile to always run a powershell -version 2?

You'll launch powershell which will launch version 2 to develop in and if there are errors in the script you'll know when they're created and commands that would run in version 5 (or whatever version your Win10 machine has) would fail.

It should be noted that launching powershell like this:
powershell -version 2 will keep the logic the same and act like the version 2 powershell but the help file and output from commmands Get-Help will still show the version 5(or whatever) syntax that is true powershell version.

Setting your Powershell Profile:
http://www.howtogeek.com/50236/customizing-your-powershell-profile/

You can check the running version with $PSVersionTable

like image 50
Ricky Cobb III Avatar answered Dec 14 '22 02:12

Ricky Cobb III