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

  • Microsoft Hires Movial To Build Android OS for Microsoft Devices

    Microsoft has hired the Romanian operations of software engineering and design services company Movial to develop an Android-based operating system solution for the Microsoft Devices business segment.

  • Microsoft Ending Workflows for SharePoint 2010 Online Next Month

    Microsoft on Monday gave notice that it will be ending support this year for the "workflows" component of SharePoint 2010 Online, as well as deprecating that component for SharePoint 2013 Online.

  • Why Windows Phone Is Dead, But Not Completely Gone

    Don't call it a comeback (because that's not likely). But as Brien explains, there are three ways that today's smartphone market leaves the door open for Microsoft to bring Windows back to smartphones.

  • Feature Update Deferral Mix-Up in Windows 10 Version 2004 Further Explained

    Microsoft last week described the confusion it is attempting to avoid by removing the client graphical user interface (GUI)-based controls to defer Windows 10 feature updates, starting with version 2004.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.