Search notes:

PowerShell cmdLet Set-StrictMode

set-strictMode is used to check the validity of expressions etc. in the current and its child scopes.
set-strictMode is set to a version (with latest referring to the most recent one):
set-strictMode -version 2
set-strictMode -version latest
set-strictMode -off
If the value of the $errorActionPreference automatic variable is set to stop, a script halts execution if a statement or an expression does not meet the requirements checked for with set-strictMode.

-version / -off

The -version argument takes one of the following four arguments
1.0 prohibits references to uninitialized variables, except for uninitialized variables in strings.
2.0 1.0 plus prohibiting references to non-existent properties (members of objects and prohibiting function calls that use the syntax for calling methods.
3.0 2.0 plus prohibiting out of bounds or unresolvable array indexes.
latest Latest version that is available
In order to disable all effects of set-strictMode, it can be set to -off.

Using set-strictMode and param statement

With a few exceptions, the param statement must be the first statement in a PowerShell script. Therefore, the set-strictMode must be placed after the param keyword.
See also statements and keywords at the top of a script

Examples

off

Setting strictMode to -off disables all checks. Undefined variables evaluate to $null without any indication of a (possible) error in the source code.
The following script prints They first said 'hello world', followed by '':
set-strictMode -off

$someText = 'hello world'

write-host "They first said '$someText', followed by '$undefinedText'"

version 1

set-strictMode -version 1

$val    = 42
$result = $val * $uninitialized_val
This script references an uninitialized variable ($uninitialized_val).
The -version 1 of the set-strictMode statement causes the script to issue the error message The variable '…' cannot be retrieved because it has not been set.

version 2

set-strictMode -version 3

[psCustomObject] $obj = @{ num = 42; txt = 'hello world' }

write-host $obj.num
write-host $obj.unobtainium
This script tries to access a property (member) of an object that does not exist ($obj.unobtainium).
With -version 2, the error message that is thrown is The property '…' cannot be found on this object. Verify that the property exists.

version 3

set-strictMode -version 3

$ary = 'foo', 'bar', 'baz'

write-host $ary[0]
write-host $ary[1]
write-host $ary[2]
write-host $ary[3]
This script creates an array with three elements. When the (inexisting) fourth element is accessed, PowerShell raises the error message Index was outside the bounds of the array.

See also

Powershell command noun: strictMode

Index