In-Depth

Understanding PowerShell's StrictMode

This command will help you to adhere to best practices when it comes to creating clear, concise code.

One of the great things about PowerShell is that it's so easy to get things done. PowerShell is a language that has a lot of syntax flexibility that allows a scripter to perform a task any number of different ways. But this flexibility can also backfire. Any language that allows it's user to code however they like can ultimately end up in many scripts that adhere to bad practices. Scripters can be as sloppy as they want to be and the code will still execute so why even mess with making it better anyway?

Even though, by default, PowerShell can allow sloppy coding practices doesn't mean you should allow it. PowerShell has a few different methods to force best practices. One of those methods is the use of the [Set-StrictMode] command. PowerShell has two different "modes" scripts can be executed in "nonstrict-mode" and "strict-mode". Although not technical terms, script execution behaves much differently when in either of these modes.

To demonstrate strict mode, let's first build some sloppy code.

## Try to use a variable without  it being declared first
Write-Host $nonExistentVariable

## Try to use an object property when it doesn't exist
$someObject = [pscustomobject]@{ SomeProperty = 'foo' }
if ($someObject.NonExistentProperty) {
Write-Host 'NonExistentProperty' exists
} else {
Write-Host 'NonExistentProperty' does not exist
}

If I have strict mode off and run this, I get the following output with no errors.

NonExistentProperty does not exist

Although this "works," it's not good practice. Instead, we a turn on strict mode and try to run this messy code again.

The variable '$nonExistentVariable' cannot be retrieved because it has not been set.

At line:2 char:12
+ Write-Host $nonExistentVariable
+            ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (nonExistentVariable:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined

The property 'NonExistentProperty' cannot be found on this object. Verify that the property exists.
At line:6 char:5
+ if ($someObject.NonExistentProperty) {
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict

Now you can see that PowerShell is enforcing best practices. It will not not allow that kind of behavior. Strict mode is enabled by using the Set-StrictMode command. This command enforces strict code either across the current console session if input interactively or across an entire module's scope if executed within a module.

Using Set-StrictMode is simple as it only has two parameters that you'll use to control it; -Off and -Version. -Off does what you might expect; turns strict mode to off and allows you to be as messy as you'd like. Using the Version parameter with the Latest value is the recommended way to write any script. Technically, the Version parameter can be either a 1 or a 2 which gives you a little control over what rules are enforced, but it's hardly ever used in the real world.

To enable strict mode, simply type Set-StrictMode -Version Latest to enable it. Get in the habit of putting this line in a script or module template at the top. There's no harm in adding it to every one of your scripts and modules, and you'll have a mechanism to force your best practices when you start to get lazy.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


Featured

comments powered by Disqus

Subscribe on YouTube