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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With