Powershell Best Practice #11: Use Set-StrictMode in your dev scripts

By | June 7, 2015

Best Practice: It is recommended to use Set-StrictMode for your development scripts to identify code issues.

Explanation:

It is the equivalent of the well known “Option Explicit” in VBS and useful to avoid to enforce coding rules (undeclared variables, etc.) and prevent some common scripting errors.

Here we can see we get an error as the variable $i has not been set.

powershell set strictmode latest version

Using “Set-StrictMode” will avoid you to waste some debugging time (to investigate unexpected results) and reduce bugs.
The parameter -Version is the most strict.

It is recommended to use Set-StrictMode during the development but not in production scripts.

Note: Set-StrictMode is turned off by default.


previous-buttonnext-button

2 thoughts on “Powershell Best Practice #11: Use Set-StrictMode in your dev scripts

  1. Pingback: Powershell Best Practice #10: Avoid empty Catch block | Powershell Guru

  2. acemary

    Know it’s going to cause more issues :-0
    But went and Set-StrictMode -Version Latest in my PoSh Profile
    Get my butt to use better coding practices 🙂

    Reply

Leave a Reply

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