Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will walk through each PowerShell command every week, explaining when and how to use them. Adam will be covering Set-StrictMode this week.

When should you use Set-StrictMode
The Set-StrictMode cmdlet sets strict mode for the current and all child scopes and turns it on or off. PowerShell will generate a terminating error if PowerShell is in strict mode. This happens when the content of an expression or script block violates best-practice coding guidelines.
To determine which coding rules are being enforced, use the -Version parameter.
NOTE: Set-PSDebug-Strict cmdlet switches on strict mode for global scope
Set-StrictMode only affects the current scope and its children scopes. You can use it in a function or script to override the setting that was inherited from the global scope.
PowerShell will display the following behaviors when Set-StrictMode has been turned off
Uninitialized variables are assumed have a value of $Null or 0 (zero), depending on the type
Referring to non-existent properties returns $Null
The error conditions can cause results of incorrect function syntax to vary.
An invalid index in an array is used to attempt to retrieve a value. This returns $Null
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How to use Set-StrictMode
As version 1.0, turn on strict mode
# By default, strict mode is disabled
$a -gt 7
Set-StrictMode -Version 1.0
$a -gt 7

With strict mode set at version 1.0, any attempts to reference variables not initialized will fail.
As version 2.0, turn on strict mode
# By default, strict mode is disabled
{function add ($a, $b) Function add ($a,$b)
‘$a = ‘ + $a
‘$b = ‘ + $b
‘$a+$b = ‘ + ($a + $b)
Add 3-4
Set-StrictMode -Version 2.0

This command activates strict mode and sets it to version 2.0. PowerShell will return an error if you use method syntax. This is a syntax that uses parentheses or commas to call functions or refer to uninitialized variables and non-existent properties.
If version 2.0 is not in strict mode, the “(3,4)” value is interpreted as an array object to which nothing has been added.
It is incorrectly interpreted as a faulty syntax to submit two values by using version 2.0 strict mode

Without version 2.0, a reference to the Month property of a string will return $Null. It is correctly interpreted as a reference error if you use version 2.0
As version 3.0, turn on strict mode
NOTE: If strict mode is set to Off, invalid and out of bounds indexes will return null values.
# By default, strict mode is disabled
$a = @(1)
$a[2] -eq $null
$a[‘abc’] -eq $null
Set-StrictMode -Version 3.0
$a = @(1)
$a[2] -eq $null
$a[‘abc’] -eq $null

If you set strict mode to version 3 or higher, errors can result from invalid or out-of-bounds indexes.
Clear-History – Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.