Set-StrictMode

Understanding Set-StrictMode in PowerShell

Welcome back to Wahmans PowerShell Blog! Today we’re going to explore one of the lesser-used yet incredibly powerful cmdlets in PowerShell: Set-StrictMode. By the end of this post, you’ll understand how Set-StrictMode can help enforce better scripting habits and catch common mistakes much earlier in your code. Whether you’re new to PowerShell or have written many scripts, Set-StrictMode can elevate the quality of your PowerShell scripting.

What is Set-StrictMode?

The Microsoft documentation describes Set-StrictMode as: Establishes and enforces coding rules in expressions, scripts, and script blocks. Essentially, this cmdlet helps you write cleaner, more predictable scripts by throwing errors on common mistakes such as referencing undeclared variables, calling methods with incorrect arguments, or using uninitialized object properties.

Basic Syntax

Set-StrictMode -Version [1.0 | 2.0 | 3.0 | Latest]
  • 1.0 – Detects uninitialized variables.
  • 2.0 – Adds support for detecting non-existent properties and arguments.
  • 3.0 – Adds detection for ambiguous function calls.
  • Latest – Applies the latest behavior available in your PowerShell version.

Example 1: Beginner – Catching Uninitialized Variables

Set-StrictMode -Version 1.0

# This will throw an error because $name is not initialized
Write-Host "Hello $name"

Without Set-StrictMode, this would simply output “Hello ” and not make it obvious that something went wrong. With strict mode enabled, PowerShell will immediately stop and notify you that the variable wasn’t initialized.

Example 2: Intermediate – Detecting Non-existent Properties

Set-StrictMode -Version 2.0

$obj = New-Object PSObject

# This will throw an error because 'Age' doesn't exist
$obj.Age = 30

In version 2.0, strict mode includes detection for properties that don’t exist on an object. This can help prevent typos or incorrect assumptions in your code.

Example 3: Advanced – Ambiguous Function Calls

Set-StrictMode -Version 3.0

function Test-Sum($a, $b) {
    return $a + $b
}

Test-Sum 5

Version 3.0 adds more strictness by checking for ambiguous or incorrect function usage. In the example above, we didn’t provide the second parameter, and thus it will generate an error, helping ensure that you’ve defined your function calls correctly.

Example 4: Expert – Using StrictMode in Script Blocks

Invoke-Command -ScriptBlock {
    Set-StrictMode -Version Latest

    # This will throw because $someVar has not been initialized
    $msg = "Value is: $someVar"
    Write-Output $msg
}

Even in remote sessions or script blocks, Set-StrictMode provides enforcement. This allows you to apply strict rules in scoped environments, such as in parallel jobs or remote PowerShell commands.

Final Thoughts

Set-StrictMode is excellent for both learning and enforcing better coding practices in PowerShell. Whether catching typos, enforcing strong parameter usage, or simply reducing those pesky silent failures, it’s a great tool to have in your scripting toolkit.

Pro tip: If you’re developing reusable modules or scripts, consider enabling Set-StrictMode -Version Latest at the top of your script. This way, bugs are caught early, and you’re forced to write more predictable and reliable PowerShell code.

Happy scripting, and I will see you in the next post!

Leave a Reply

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