PowerShell Windows

PowerShell Best Practices: Set-StrictMode

During some research for PowerShell best practices I came across the following blog post on the Hey Scripting Guy Blog from Microsoft:

By using the Set-StrictMode cmdlet, we can enforce some common “best practice” coding techniques that you can use with your scripts. In yesterday’s post, I used the –Strict parameter with Set-PSDebug. This enforces the variable declaration in a script. But with Set-StrictMode, we get that plus a little more. The scope is limited to the current scope and child scopes. By default, Set-StrictMode is turned off to prevent error messages while you are writing the script.

From now on, all my PowerShell scripts will start with “Set-StrictMode -Version latest” and “Set-PSDebug -Strict”:

Set-PSDebug -Strict
Set-StrictMode -Version latest
You may also like
Disable IPv6 in Windows for a specific network connection only
Get running processes including CPU and memory usage
VBScript: Change Word Document Template

Leave Your Comment

Your Comment*

Your Name*
Your Webpage