Testing your scripts in earlier version of PowerShell on the same device

Ever since the first bits of PowerShell v3 were publicly released, I started using them.
Since then I’ve ran into some errors when running those scripts in a production environment where they are using PowerShell v2 or, god forbid, PowerShell v1. This is because PowerShell v3 has a lot, and I do mean a lot, improvements for making the command line easier as well with entire scripts.
Note: More on those improvements in some of my upcoming posts.

So, to avoid having to test scripts on a device with an earlier version of PowerShell installed instead of, for example my own laptop which has PowerShell v3, one can use the “Set-StrictMode” cmdlet.

Now what is the Set-StrictMode cmdlet? Simply put, it allows you to revert your installed PowerShell back to an earlier version Emoticon met brede lach And just to make things clear, it reverts the functionalities back… it doesn’t re-install or something like that. Knipogende emoticon

So, if you’re writing a script and want to test it in PowerShell v2 but the device has PowerShell v3 installed, simply put the following code at the beginning of your script:

Set-StrictMode –Version 2

One comment

  1. Marvin D. says:

    You’ve understood the functionality of Set-ScrictMode totally wrong. This cmdlet has nothing to do with earlier versions of PowerShell. Instead, it enforces specific coding styles.

Leave a Reply

Your email address will not be published. Required fields are marked *