Reputation: 23395
I currently have Windows Management Framework 5.1 installed. I want to test that my script works correctly with PowerShell 3 and 4. I am aware of the PowerShell.exe -Version
switch but when I try:
PowerShell.exe -Version 3
Or:
PowerShell.exe -Version 4
And then run $PSVersionTable
it still shows PowerShell version 5.1 as the running version.
If I run:
PowerShell.exe -Version 2
Then $PSVersionTable
does show that the console is now version 2. Why is this?
Upvotes: 11
Views: 16792
Reputation: 23395
Windows PowerShell provides a special -Version 2.0
backwards compatibility mode that allows you to run PowerShell version 2 even when you have later versions installed:
The same is not true for all other versions of PowerShell, it is unfortunately not possible to force PowerShell to run as version 3 or 4 when PowerShell version 5 is installed (even though running PowerShell.exe -version 3 will not throw any kind of error/warning message). It is also not possible to have multiple PowerShell versions installed at the same time (with the exception of PowerShell Core which can be installed at the same time as Windows PowerShell).
The only way to test PowerShell scripts with a version of PowerShell (other than 2) is to have alternative installs of PowerShell on separate computers or Virtual Machines.
Windows PowerShell 4.0 and Windows PowerShell 3.0 are designed to be backwards compatible with Windows PowerShell 2.0. Cmdlets, providers, snap-ins, modules, and scripts written for Windows PowerShell 2.0 run unchanged in Windows PowerShell 4.0 and Windows PowerShell 3.0. However, due to a change in the runtime activation policy in Microsoft .NET Framework 4, Windows PowerShell host programs that were written for Windows PowerShell 2.0 and compiled with Common Language Runtime (CLR) 2.0 cannot run without modification in Windows PowerShell 3.0 or Windows PowerShell 4.0, which are compiled with CLR 4.0. The Windows PowerShell 2.0 Engine is intended to be used only when an existing script or host program cannot run because it is incompatible with Windows PowerShell 4.0, Windows PowerShell 3.0, or Microsoft .NET Framework 4. Such cases are expected to be rare.
Upvotes: 17