Set-StrictMode| Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Set-StrictMode.

When to use Set-StrictMode?

The Set-StrictMode cmdlet configures strict mode for the current scope and all child scopes, and turns it on and off. When strict mode is on, PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.

Use the –Version parameter to determine which coding rules are enforced.

NOTE: Set-PSDebug -Strict cmdlet turns on strict mode for the global scope.

Set-StrictMode affects only the current scope and its child scopes. Therefore, you can use it in a script or function to override the setting inherited from the global scope.

When Set-StrictMode is off, PowerShell has the following behaviors:

  • Uninitialized variables are assumed to have a value of 0 (zero) or $Null, depending on type
  • References to non-existent properties return $Null
  • Results of improper function syntax vary with the error conditions
  • Attempting to retrieve a value using an invalid index in an array returns $Null

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Set-StrictMode?

Turn on strict mode as version 1.0:

# Strict mode is off by default.

$a -gt 7

Set-StrictMode -Version 1.0

$a -gt 7

With strict mode set to version 1.0, attempts to reference variables that are not initialized fail.

Turn on strict mode as version 2.0:

# Strict mode is off by default.

function add ($a, $b) {

‘$a = ‘ + $a

‘$b = ‘ + $b

‘$a+$b = ‘ + ($a + $b)

}

add 3 4

add(3,4)

Set-StrictMode -Version 2.0

add(3,4)

This command turns strict mode on and sets it to version 2.0. As a result, PowerShell returns an error if you use method syntax, which uses parentheses and commas, for a function call or reference uninitialized variables or non-existent properties.

Without version 2.0 strict mode, the “(3,4)” value is interpreted as a single array object to which nothing is added.

By using version 2.0 strict mode, it is correctly interpreted as faulty syntax for submitting two values.

Without version 2.0, the reference to the non-existent Month property of a string returns only $Null. By using version 2.0, it is interpreted correctly as a reference error.

Turn on strict mode as version 3.0:

NOTE: With strict mode set to Off, invalid or out of bounds indexes result return null values.

# Strict mode is off by default.

$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

With strict mode set to version 3 or higher, invalid or out of bounds indexes result in errors.

Learn last week’s command: Clear-History.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *