Establish and enforce coding rules in expressions, scripts, and script blocks.

      Set-StrictMode -Off [CommonParameters]

      Set-StrictMode -Version Version [CommonParameters]

       Turn strict mode off.
       This parameter also turns off "Set-PSDebug -Strict".

    -Version Version
       The conditions that cause an error in strict mode. (Required)

       valid values:

        1.0   Prohibit references to uninitialized variables,
              except for uninitialized variables in strings.

        2.0   Prohibit references to uninitialized variables (including uninitialized variables in strings).
              Prohibit references to non-existent properties of an object.
              Prohibit function calls that use the syntax for calling methods.
              Prohibit a variable without a name (${}).

        3.0   Prohibits references to uninitialized variables. This includes uninitialized variables in strings.
              Prohibits references to non-existent properties of an object.
              Prohibits function calls that use the syntax for calling methods.
              Prohibit out of bounds or unresolvable array indexes.

      Latest  Select the latest (most strict) version available.
              Use this value to assure that scripts use the strictest
              available version, even when new versions are added to PowerShell.

With strictmode set to 2.0 calling a function with myfunct(2,4) instead of the correct myfunct 2 4 will throw an error "The function or command was called like a method. Parameters should be separated by spaces"

Set-StrictMode is effective only in the scope in which it is set and in its child scopes.


Turn strict mode on and set it to version 1.0:

PS C:\> set-strictmode -version 1.0

PS C:\> $a -gt 5
The variable $a cannot be retrieved because it has not been set yet.
At line:1 char:3

Turn off strictmode (this is the default):

PS C:\> set-strictmode -off

“You are already of consequence in the world if you are known as a man of strict integrity” ~ Grenville Kleiser

Related PowerShell Cmdlets

Set-PSdebug - Turn script debugging on or off.
Equivalent bash command: set - Set shell options to control error handling in a script.

Copyright © 1999-2024
Some rights reserved