-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
.
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.