Testing your PowerShell code in different versions from the one workstation

I hit a brick wall today while smashing together various PowerShell code snippets for a script I am working on (that I’ll share later). I had my code nice and clean, commented, versioned and working…until I decided to test against a earlier version of Windows whereby my excitement was promptly shattered (thanks Windows 7).

A good lesson from this is to always test, test and do more tests before you pop the cork on the champagne.

But having multiple dev workstations are a royal pain to maintain, even in the glorious era of virtualisation.

Thankfully Microsoft have made this somewhat easier with the ability to direct PowerShell what version it should be running in. Once you have your code ready to go (or your are going to freestyle in the shell) open up a run prompt and enter the following:

powershell.exe -version x

Just replace the with the numerical version of PowerShell you want to run and TADA, you will find yourself in a PowerShell version of your choosing:

This method can also be used when you are calling scripts in a shortcut, SCCM package or other places good PowerShell scripts are found via the same method, for example:

powershell.exe -version 2 .\fancyscript.ps1

Now you can test your code to your hearts content from the one workstation without too much effort.

And if you have forgotten what version of PowerShell comes with what version of Windows, take a look below:

PowerShell Version Default Windows Versions
PowerShell 2.0 Windows 7, Windows Server 2008 R2
PowerShell 3.0 Windows 8, Windows Server 2012
PowerShell 4.0 Windows 8.1, Windows Server 2012 R2
PowerShell 5.0 Windows 10
PowerShell 5.1 Windows 10 Anniversary Update, Windows Server 2016

Enjoy

James Written by:

  • Elijah Gagne

    Thanks for this. I can do what you show as an example and get back to version 2 by running “powershell.exe -version 2.” However, I cannot seem to get to versions 3, 4, or 5.0. When I try running:

    powershell.exe -version 3
    powershell.exe -version 4
    powershell.exe -version 5

    I always get version 5.1.

    • Nimal Raj

      You also need to have the specific version of PowerShell installed on the system for this to work. In other words, the above will only work if your upgrade path is PS 2
      > 3 > 4 > 5. Otherwise you’ll just go back and forth between
      the versions available on the machine. It sound like you upgraded from PowerShell 2.x to PowerShell 5.1 on this system.

      • A little research suggests you can only have one version of PowerShell installed on a machine. However, version two is a strange exception and is incorporated into each version of PowerShell beyond 2.0.
        This would explain why you can only bounce between version 2.0 and the latest version installed on your workstation

      • From what I can tell; it appears you can only have one version of PowerShell installed at any one time. However, version 2.0 has been incorporated (weirdly) in all versions since. This is why you can bounce between the latest version of PowerShell on your workstation (whether it be v3, v4 or v5) and version 2.

        Hopefully it’s still a useful switch to know.