CmdID / PowerShell / Cmdlets / Set
Set-StrictMode
Establishes and enforces coding rules in expressions, scripts, and scriptblocks. PowerShell reference
From the PowerShell reference 2 parameters
What it does
Establishes and enforces coding rules in expressions, scripts, and scriptblocks. 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, PowerShell generates a terminating error when the content of an expression, script, or scriptblock violates basic best-practice coding rules. Use the Version parameter to determine the coding rules to enforce. Set-PSDebug -Strict cmdlet turns on strict mode for the global scope. Set-StrictMode affects only the current scope and its child scopes. Then, you can use it in a script or function to override the setting inherited from the global scope. When Set-StrictMode is off, PowerShell has the following behaviors: - Uninitialized variables are assumed to have a value of 0 (zero) or $null, depending on type - References to non-existent properties return $null - Results of improper function syntax vary with the error conditions - Attempting to retrieve a value using an invalid index in an array returns $null
Set-StrictMode -Version <Version> [<CommonParameters>]Set-StrictMode [-Off] [<CommonParameters>]
Turn on strict mode as version 1.0
# Strict mode is off by default.
$a -gt 5IdentifyTurn on strict mode as version 2.0
# Strict mode is off by default.
function add ($a, $b) {
'$a = ' + $a
'$b = ' + $b
'$a+$b = ' + ($a + $b)
}
add 3 4IdentifyTurn on strict mode as version 3.0
# Strict mode is off by default.
$a = @(1)
$null -eq $a[2]
$null -eq $a['abc']IdentifyIts page in the reference: Set-StrictMode.
Parameters
-OffIndicates that this cmdlet turns strict mode off for the current scope and all child scopes.-VersionSpecifies the conditions that cause an error in strict mode. This parameter accepts any valid PowerShell version number. Any number higher than 3 is treated as Latest. The value supplied must be the string Latest or a string that can be converted to a System.Version type. The version must match a valid release version of PowerShell.
The effective values for this parameter are:
- 1.0
- Prohibits references to uninitialized variables, except for uninitialized variables in strings.
- 2.0
- Prohibits references to uninitialized variables. This includes uninitialized variables in
strings.
- Prohibits references to non-existent properties of an object.
- Prohibits function calls that use the syntax for calling methods.
- 3.0
- Prohibits references to uninitialized variables. This includes uninitialized variables in
strings.
- Prohibits references to non-existent properties of an object.
- Prohibits function calls that use the syntax for calling methods.
- Prohibit out of bounds or unresolvable array indexes.
- Latest
- Selects the latest version available. The latest version is the most strict. Use this value to …