Set-StrictMode - PowerShell command help and examples

Establishes and enforces coding rules in expressions, scripts, and script blocks. (Set-StrictMode)


NAME
Set-StrictMode
SYNOPSIS
Establishes and enforces coding rules in expressions, scripts, and script blocks.
SYNTAX
Set-StrictMode -Off [<CommonParameters>] Set-StrictMode -Version <Version> [<CommonParameters>]
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.
PARAMETERS
-Off [<SwitchParameter>] Turns strict mode off. This parameter also turns off "Set-PSDebug -Strict". Required? true Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -Version <Version> Specifies the conditions that cause an error in strict mode. This parameter is required. The valid values are "1.0", "2.0", and "Latest". The following list shows the effect of each value. 1.0 -- Prohibits references to uninitialized variables, except for uninitialized variables in strings. 2.0 -- Prohibits references to uninitialized variables (including uninitialized variables in strings). -- Prohibits references to non-existent properties of an object. -- Prohibits function calls that use the syntax for calling methods. -- Prohibits a variable without a name (${}). Latest: --Selects the latest (most strict) version available. Use this value to assure that scripts use the strictest available version, even when new versions are added to Windows PowerShell. Required? true Position? named Default value None Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type, "get-help about_commonparameters".
INPUTS
None You cannot pipe input to this cmdlet.
OUTPUTS
None This cmdlet does not return any output.
NOTES
Set-StrictMode is similar to the Strict parameter of Set-PSDebug. "Set-Strictmode -version 1" is equivalent to "Set-PSDebug -strict", except that Set-PSDebug is effective in all scopes. Set-StrictMode is effective only in the scope in which it is set and in its child scopes. For more information about scopes in Windows PowerShell, see about_Scopes.

Examples

EXAMPLE 1
C:\PS>set-strictmode -version 1.0 C:\PS> $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
Description
----------- 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
C:\PS># set-strictmode -version 2.0 # Strict mode is off by default. C:\PS> function add ($a, $b) {$a + $b} C:\PS> add 3 4 7 C:\PS> add(3,4) 3 4 C:\PS> set-strictmode -version 2.0 C:\PS> 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 C:\PS> set-strictmode -off C:\PS> $string = "This is a string". C:\PS> $string.Month C:\PS> C:\PS> set-strictmode -version 2.0 C:\PS> $string = "This is a string". C:\PS> $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
Description
----------- 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. RELATED LINKS Online version: http://go.microsoft.com/fwlink/?LinkID=113450 about_Debuggers about_Scopes Set-PSDebug C:\Windows>powershell get-help New-Module -full

Microsoft Windows [Version 10.0.19045.3693]
Copyright (c) 2023 Microsoft Corporation.

ColorConsole [Version 3.7.1000] PowerShell 2.0-Export

Windows 11, 10, 8.1, 8, 7 / Server 2022, 2019, 2016











Windows-10


... Windows 10 FAQ
... Windows 10 How To


Windows 10 How To


... Windows 11 How To
... Windows 10 FAQ



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

HTTP: ... PS_Windows/en/Set-StrictMode.htm
0.062
17430
Create Action Center Desktop Shortcut for Windows 8.1/8!
Start the Device Manager on Windows 8, 8.1 and Win 10 (open, find, run)!
Wie erzwinge ich den Standby, ich will in Windows 11,10, ... nicht immer zum Ruhezustand?
Fragen zu WinScan2PDF für Windows?
How to open in Windows 8.1/10 the program path through the task manager?
Geschützte Ordner Problem bei portabler Einsatz in der Scansoftware?
Gibt es so nen Halter fürs Handy den man umhängen kann?
Speichern der Scans als JPEG-Dateien in WinScan2PDF, aber wie?
Can I create a desktop shortcut for the Windows 8, 8.1 / 10 onscreen keyboard?
Open and start Internet Explorer always on Windows 8.1 / 10 desktop (disable Metro App)!



(0)