3

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?

2
  • I don't know of a means to configure ISE that way, but you can at least test your script against version 2 of the command line, by running powershell -version 2. Commented Aug 31, 2016 at 18:07
  • See Start PowerShell ISE with the 2.0 runtime Commented Aug 31, 2016 at 18:21

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That'll work! I'll use the console host to verify that the commands I want to use exist before I use them in ISE.
2
Set-StrictMode -Version 2.0

MSDN Link

-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

-1

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.

1 Comment

This does not do what the poster asked. It will not restrict the commands available to only those of a certain (maximum) version, it just doesn't allow the script to execute unless a specific minimum version is available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.