7

I would like to use PowerShell 2 as the default PowerShell version on Windows 8 without specifying the -Version switch.

I started using Windows 8 RTM which comes with PowerShell 3, and I have scripts that are not compatible with PowerShell 3.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
walterdido
  • 73
  • 1
  • 4
  • May I ask what incompatibilities are there? So far I got the impression that they value backwards compatibility quite much. – Joey Aug 21 '12 at 18:01
  • I inherited an application which hosts powershell. Now there was a bug in powershell 2 regarding PipelineResultTypes Error | Output which has been resolved in Powershell 3. The application used a work-around to get by this bug. The fix in powershell 3 now breaks the scripts that were embedded in the application. – walterdido Aug 21 '12 at 18:24
  • 2
    I'm getting this error when trying to install a NuGet package within VS 2012 RTM on Windows 8 RTM: `T4Scaffolding and MvcScaffolding currently do not support preview versions of PowerShell 3` – Leniel Maccaferri Aug 23 '12 at 15:16
  • @LenielMacaferi I'm using a custom fork of MVC scaffolding (http://mvcscaffolding.codeplex.com/SourceControl/network/forks/shalter/crazierfork) to use scaffolding on a Windows 8 box. Might help you out? – Matthew Abbott Jun 26 '13 at 13:52

2 Answers2

6

Powershell uses a publisher policy (see here also) to automatically redirect hosts built against Powershell 2 onto the Powershell 3 runtime if it's available.

Most of the time this is exactly what you want, however you can explicitly disable the publisher policy if needed for your app.

Put this in your app.config file to disable the publisher policy for System.Management.Automation (powershell runtime):

<configuration>
  <runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="System.Management.Automation" publicKeyToken="31bf3856ad364e35" />
         <publisherPolicy apply="no" />
      </dependentAssembly>
     </assemblyBinding>
  </runtime>
</configuration>

Testing it out (console app targeted to .NET 4.0, with explicit reference to PS v2 runtime):

PowerShell ps = PowerShell.Create();
ps.AddScript("$psversiontable");
var result = ps.Invoke()[0].BaseObject as Hashtable;

Console.WriteLine("Powershell version: {0}", result["PSVersion"]);
Console.WriteLine(".NET version: {0}", typeof(string).Assembly.GetName().Version);

Running this on my Win8 box (PSv3 definitely there), I get result of

Powershell version: 2.0
.NET version: 4.0.0.0

And PS version goes to 3.0 if I comment out app.config.

latkin
  • 16,402
  • 1
  • 47
  • 62
  • This worked great for me, I also had to disable the publisher policy on other relevant DLLs before it would stop picking up the redirect. – Nicholas W Nov 20 '13 at 17:39
  • Additional instructions on which DLLs that also need to have publisher policy application blocked is available here: http://stackoverflow.com/a/12777107/3063884 – CJBS Feb 05 '14 at 23:04
0

You say you have a custom application that hosts PowerShell and that the scripts are embedded in the application. Let's assume this application is called "MyApp.exe." Now, PowerShell 3.0 requires .NET 4.0 (CLR4), so the way to force the application to use PowerShell 2.0 is to force your application to use .NET 3.5 (CLR2). This will cause it to load the 1.0.0.0 version of System.Management.Automation instead of the 3.0.0.0 version which is for CLR4 only. Create a "MyApp.exe.config" in the same folder as the application with the following contents:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
 <startup>
  <supportedRuntime version="v2.0.50727"/>
 </startup>
</configuration>

Now, if your application was built against CLR4 and PowerShell 2.0, then you're in a bit more trouble. Hopefully this is not the case.

x0n
  • 51,312
  • 7
  • 89
  • 111