I'm writing a Powershell script (on a Win10 machine) that needs to be backward-compatible all the way to Win7 machines - which shipped with Powershell Version 2.0. I'd like to restrict my command set in the ISE to only those commands available in 2.0, or throw an error if I stray outside that version or something while I'm writing the script - is there an easy way to do this?
3 Answers
I don't know if there's a way in the ISE (I think not), but you can start the prompt (console host) separately in 2.0 mode:
powershell.exe -Version 2.0
You could use this in conjunction with ISE by editing and saving the script in ISE, then executing it in the console host. Not ideal, but not too bad of a workaround.
1 Comment
Set-StrictMode -Version 2.0
-Version 2.0
- Prevents use of variables that have not been initialized (think Option Explicit in VBScript)
- Cannot call non-existent properties on objects
- Disallows calling a function like a method, for example, Do-Something(1 2)
- Prohibits creating variables without a name
Note, you can use it to restrict some functions for specific methods.
About the commandlets. Here no any solutions.
I saggest you to check every commandlet by using Get-Help. There you can get version in description.
Example:
Get-Help -Name "Invoke-RestMethod"
In answer: ... This cmdlet was introduced in Windows PowerShell 3.0. ...
Comments
You can also include the requires directive at the top of the script itself.
#requires -version 5
Use the correct number for the version your script requires, 2, 3, 4, etc. This will ensure it works in the ISE.
powershell -version 2.