Powershell Best Practice #11: Use Set-StrictMode

By | June 7, 2015

Best Practice: It is recommended to use Set-StrictMode.

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.

There are different cases when using undeclared variables can be dangerous because you can get unexpected results without being aware of that immediately.

Using “Set-StrictMode” will avoid you to loose some time when debugging and investigating why the result you got is not the expected one.

So, trying to use an undeclared variable will return an error that the variable has not been set.

The parameter -Version specifies the condition that cause an error (ex: 1.0, 2.0, Latest).

Note: Set-StrictMode is turned off by default.


previous-buttonnext-button

2 thoughts on “Powershell Best Practice #11: Use Set-StrictMode

  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 *