0

Is it possible to run powershell passing it the version of the .NET CLR we require?

Systems using Powershell V2 use CLR version 2 by default, and we need version 4.

We could implement a configuration file to:

  • C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe.config
  • C:\Windows\System32\WindowsPowerShell\v1.0\PowerShellISE.Exe.config
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShell.exe.config
  • C:\Windows\SysWOW64\WindowsPowerShell\v1.0\PowerShellISE.exe.config

Like this:

<configuration> 
<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0.30319"/> 
    <supportedRuntime version="v2.0.50727"/> 
</startup> 
</configuration>

But we don't want make changes on customers servers which may potentially break other programs which rely on PowerShell.

Community
  • 1
  • 1
Tim
  • 7,401
  • 13
  • 61
  • 102
  • [This question](http://stackoverflow.com/a/5403227/517852) has some interesting answers, in particular [this one](http://stackoverflow.com/a/5403227/517852) about using activation configuration files for one-off invocations using .NET 4. – Mike Zboray May 14 '15 at 18:22

1 Answers1

1

You could check the CLR Version in your script and exit with an error if the minimum requirements are not met:

if ($PSVersionTable.CLRVersion.Major -lt 4) {
  Write-Error 'CLR Version 4.0 or newer required.'
  exit 1
}

Alternatively, if checking for a minimum PowerShell version would also work, you could put a #requires directive as the first line in your script:

#requires -version 4.0
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328