drohm
drohm

Reputation: 224

PowerShell's '-Version' parameter and CLR versions

I have installed the release version of PowerShell v3 and am having some trouble getting the -Version parameter to work. I have quite a few modules that require .NET 4.0, and I have been using PowerShell v2 without issue using the following powershell.exe.config file:

<?xml version="1.0"?>
<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0.30319"/>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
    <runtime>
        <loadFromRemoteSources enabled="true"/>
    </runtime>
</configuration>

This would allow me to run PowerShell v2 with the .NET 4.0 CLR. Everything worked without issue.

I have now installed the release version of PowerShell v3, and anytime I use the command 'powershell -version 2' and check the $PSVersionTable variable, it shows that it has loaded PowerShell v3 (with .NET 4.0). If I remove the powershell.exe.config file above and run the same command, it shows that PowerShell v2 is loaded but is using .NET 2.0.

So my question is this: Is there a way to load PowerShell v2 and have it use the .NET 4.0 CLR like I used to do before installing PowerShell v3? Or does PowerShell v3 not allow this?

Also, I should mention why I'm trying to do this - mainly in case there are any incompatibility issues that might crop up when we switch to V3. I plan to test everything with V3, but having the ability to drop back to V2 can save us from a lot of headaches.

Upvotes: 2

Views: 1824

Answers (2)

latkin
latkin

Reputation: 16792

What's happening is the following:

  • Start with -Version 2
  • Powershell starts, beings to bind to v2 runtime/assemblies. So far so good.
  • App.config indicates that .NET 4 runtime should be loaded
  • As assembly binding progresses, there is a .NET publisher policy that automatically redirects any app built against v2 to v3 when possible
    • This is normally what you want - if new version is available, automatically use it
  • Result is v3 runtime/assemblies are loaded, despite your original intention (and Powershell's) to run v2.
    • This does not happen without the .NET 4 workaround, since v2 will load under .NET 2 by default, and thus it cannot be redirected by the publisher policy onto v3, which requires .NET 4.

Simple, right?!

So what you need to do, if you want to keep PSv2 on .NET 4, is manually disable the publisher policy. This can be done in the app.config file, see a related post here.

Upvotes: 5

x0n
x0n

Reputation: 52430

I don't understand why you want to use v2 with CLR 4 if you have v3 installed. If your modules require .NET 4, use PowerShell v3: It uses CLR 4 already.

Upvotes: 0

Related Questions