Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run PowerShell in version 3 or 4 mode when PowerShell 5 is installed?

Tags:

powershell

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?

like image 908
Mark Wragg Avatar asked Jun 09 '17 12:06

Mark Wragg


People also ask

Can you have multiple versions of PowerShell installed?

PowerShell 3.0 and PowerShell 4.0 only supported one version of a module. Starting in PowerShell 5.0, modules are installed in <modulename>\<version> . This allows you to install multiple versions side-by-side.

Can PowerShell 3.0 and 4.0 run on the same computer?

Windows PowerShell 4.0 is designed to be backwards compatible with Windows PowerShell 3.0 and Windows PowerShell 2.0. Cmdlets, providers, snap-ins, modules, and scripts written for Windows PowerShell 2.0 and Windows PowerShell 3.0 run unchanged in Windows PowerShell 4.0.

Is PowerShell 5 backwards compatible?

Windows PowerShell is forward compatible. This means a script that I wrote in Windows PowerShell 1.0 will run without problems in Windows PowerShell 5.0.

Are PowerShell versions backwards compatible?

Although it is true that its succeeding versions provide better features, Windows PowerShell still has one advantage that the newer PowerShell doesn't—complete backward compatibility. The first PowerShell was released on November 14, 2006. This was when Windows Vista and XP were still the latest Windows OS.


1 Answers

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.

  • https://msdn.microsoft.com/en-us/powershell/scripting/setup/starting-the-windows-powershell-2.0-engine
like image 119
Mark Wragg Avatar answered Oct 19 '22 09:10

Mark Wragg