Using Strict Mode in PowerShell

PowerShell is incredibly flexible; it’s a forgiving scripting language. But this could turn into scripts not written according to best practices or that rely on things that work automatically, and sometimes fail without really understanding why. In this small blog post, I will show you how Strict mode works and how it can help you 🙂

What is Strict Mode?

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 scriptblock violates basic best-practice coding rules.

Use the Version parameter to determine the coding rules to enforce.

Set-PSDebug -Strict cmdlet turns on strict mode for the global scope. Set-StrictMode affects only the current scope and its child scopes. Then, 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

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode?view=powershell-7.5

How to use

Versions

By default, Strict Mode is off. You can turn it on in your script by using Set-StrictMode -Version:Latest.

and you can turn it off again, or start a new session in which it will be off by default, by running Set-StrictMode -Off:

The Version:Latest tells it to use the latest version, but you can specify these:

  • 1.0
    • Prohibits references to uninitialized variables, except for uninitialized variables in strings.
  • 2.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.
  • 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
    • Selects the latest version available. The latest version is the most strict. Use this value to make sure that scripts use the strictest available version, even when new versions are added to PowerShell.

This was taken from the same Microsoft Learn page as in the first chapter, but it also has a red caution note:

“Using Latest for Version in scripts isn’t deterministic. The meaning of Latest can change in new releases of PowerShell. A script written for an older version of PowerShell that uses Set-StrictMode -Version Latest is subject to more restrictive rules when run in a newer version of PowerShell.”

Note: There is no Get-StrictMode cmdlet to show you the current strict mode in your session. Remember: it will be off by default for each new session you start.

Examples

Accessing unset Variables

When you try to access a variable that hasn’t been set, PowerShell returns no error or indication of why it’s empty, unless Strict Mode is enabled. But it will help you understand a bit more if you do 🙂 For example:

Comparing values to Variables

If you want to query an Array, using Strict Mode will give you more information about why you can’t access some parts of it. For example:

The Number Variable, an Array containing only one Value, can be queried to return its first value using $Number[‘0’]. Querying a second value, using $Number[‘1’] doesn’t return anything because there is no second Value and using $Number[‘a’] doesn’t return anything because that’s not how you can search for Values in an Array 🙂 Using Strict-mode, $Number[‘0’] returns 1337, $Number[‘1’] doesn’t return anything, but does show you that it’s out of bounds, meaning that there are no more values. Using $Number[‘a’] tells you that you can’t use letters but must use numbers 🙂

PSCustomObjects

Still one of my favorite things in PowerShell, PSCustomObjects. I use them to store data in, create reports, etc. But sometimes you try to access a value in it that you didn’t fill in or that you mistyped. For example:

The $Total Variable is a PSCustomObject with three Properties: Name, Size, and Date. I mistyped Name as Nme, and normally, without Strict Mode, it would return nothing, no error, and you might read past the mistyped Property. With Strict Mode on, you get an error saying it can’t find the mistyped Property and that you should verify it. Much more helpful 🙂

Wrapping up

And that’s how Strict Mode works and how it can help you better understand why things work, or don’t work 😉 Have a lovely weekend!

One thought on “Using Strict Mode in PowerShell

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.