PowerShell Logo Small

Set-StrictMode



This is the built-in help made by Microsoft for the command 'Set-StrictMode', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Establishes and enforces coding rules in expressions, scripts, and script blocks.

SYNTAX


Set-StrictMode -Version <Version> [<CommonParameters>]
Set-StrictMode -Off [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


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, Windows PowerShell generates a
terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.


Use the Version parameter to determine which coding rules are enforced.


Unlike the Set-PSDebug cmdlet, Set-StrictMode affects only the current scope and its child scopes, so you can use it in a script or function without affecting the global
scope.


When Set-StrictMode is off, uninitialized variables (Version 1) are assumed to have a value of 0 (zero) or $null, depending on type. References to non-existent properties
return $null, and the results of function syntax that is not valid vary with the error. Unnamed variables are not permitted.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=289614
Set-PSDebug
about_Scopes
about_Debuggers

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>set-strictmode -version 1.0
PS C:\>$a -gt 5
False
The variable $a cannot be retrieved because it has not been set yet.
At line:1 char:3
+ $a <<<< -gt 5
+ CategoryInfo : InvalidOperation: (a:Token) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined



This command turns strict mode on and sets it to version 1.0. As a result, attempts to reference variables that are not initialized will fail.

The sample output shows the effect of version 1.0 strict mode.










-------------------------- EXAMPLE 2 --------------------------

PS C:\># set-strictmode -version 2.0
# Strict mode is off by default.

PS C:\>function add ($a, $b) {$a + $b}
PS C:\>add 3 4
7
PS C:\>add(3,4)
3
4
PS C:\>set-strictmode -version 2.0
PS C:\>add(3,4)

The function or command was called like a method. Parameters should be separated by spaces, as described in 'Get-Help about_Parameter.'
At line:1 char:4
+ add <<<< (3,4)
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : StrictModeFunctionCallWithParens

PS C:\>set-strictmode -off
PS C:\>$string = "This is a string."
PS C:\>$string.Month
PS C:\>
PS C:\>set-strictmode -version 2.0
PS C:\>$string = "This is a string."
PS C:\>$string.Month

Property 'month' cannot be found on this object; make sure it exists.
At line:1 char:9
+ $string. <<<< month
+ CategoryInfo : InvalidOperation: (.:OperatorToken) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFoundStrict



This command turns strict mode on and sets it to version 2.0. As a result, Windows PowerShell throws an error if you use method syntax (parentheses and commas) for a
function call or reference uninitialized variables or non-existent properties.

The sample output shows the effect of version 2.0 strict mode.

Without version 2.0 strict mode, the "(3,4)" value is interpreted as a single array object to which nothing is added. With version 2.0 strict mode, it is correctly
interpreted as faulty syntax for submitting two values.

Without version 2.0, the reference to the non-existent Month property of a string returns only null. With version 2.0, it is interpreted correctly as a reference error.