6

I would like to run my powershell script in v2 mode. Is it possible to do this without having a wrapper script?

So for example, I can do this now if I can two files.

MainContent.ps1

write-output 'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit" 


Wrapper.ps1

powershell -version 2 -file 'MainContent.ps1'

This will work but I'm hoping I don't need to have this second wrapper file because I'm creating a whole bunch of these ps1 scripts, and having wrapper files will double the amount of scripts I'll need.

I'm hoping I can do something like this.

MainContent.ps1

Set-Powershell -version 2
write-output 'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit" 

Later on, I would also like each script to ask for a set of credentials as well without using a wrapper file.

Is this currently possible?

To be clear, I'm using version 2 of powershell

5
  • Pardon my curiosity, but why do you need this? Are you just trying to write portable scripts, or do you have a specific compatibility issue? Commented Jul 3, 2015 at 0:58
  • @RyanBemrose = compability issue. Users with powershell 3.0 installed get this error "Microsoft SharePoint is not supported with version 4.0.30319.17929 of the Microsoft .Net Runtime." Commented Jul 3, 2015 at 1:03
  • It's not just the PowerShell version then. It's the underlying .NET runtime you need loaded. Starting a new process is the only way to change it. Commented Jul 3, 2015 at 1:06
  • @mikez - yeah I'm not sure why that message is showing but this KB says to just change the powershell version and it does work when I do that. support.microsoft.com/en-us/kb/2796733 Commented Jul 3, 2015 at 1:08
  • PowerShell 2.0 used .NET 3.5, so it's a way force down the CLR version. Commented Jul 3, 2015 at 1:14

1 Answer 1

9

If your only goal is to avoid creating separate wrapper scripts, you can always have the script re-launch itself. The following script will always re-launch itself once with PS version 2.0.

param([switch]$_restart)
if (-not $_restart) {
  powershell -Version 2 -File $MyInvocation.MyCommand.Definition -_restart
  exit
}

'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit"

Or you can make it conditional. This script re-launches itself with version 2 only if the version is greater than 2.0.

if ($PSVersionTable.PSVersion -gt [Version]"2.0") {
  powershell -Version 2 -File $MyInvocation.MyCommand.Definition
  exit
}

'run some code'
Read-Host -Prompt "Scripts Completed : Press any key to exit"
Sign up to request clarification or add additional context in comments.

3 Comments

ah ok, that's not bad. Is this a common pattern? Is there a way to make it generic so that instead of checking the version, it checks to see if the script has run the first time? Then I can actually put other stuff in there like calling Set-Credential to get the user to run the script in a particular account
Modified to show both cases - conditional restart and always restart.
thanks ryan. That's exactly the line of logic that I was implementing, but thanks for completing it for me!

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.