Skip to main content

Testing your PowerShell code in different versions from the one workstation

·283 words·2 mins
system-administration tech powershell sysadmin technology windows
James Pettigrove
Author
James Pettigrove
Cloud Engineer with a focus on Microsoft Azure

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 x 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

Related

Ping a set of hosts with Powershell
·368 words·2 mins
system-administration tech ping powershell sysadmin technology test-connection windows
You may find yourself one day having to ping a set of machines on a regular basis.
RSAT and MDOP for Windows 10 released
·84 words·1 min
system-administration tech mdop mdop-2015 rsat sysadmin technology windows windows-10
Sysadmin’s start your engines (err… servers?
Clear WPAD client cache
·301 words·2 mins
system-administration tech internet-explorer proxy sysadmin technology windows wpad
wpad # noun [no plural] # A wonderful but horrible way to automate clients connecting to the internet via proxy server.