-
Notifications
You must be signed in to change notification settings - Fork 62
[Freshness] PSScriptAnalyzer Article Updates - Batch 1 #401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||
| --- | ||||||||||
| description: Avoid exclaim operator | ||||||||||
| ms.date: 03/26/2024 | ||||||||||
| ms.date: 05/28/2026 | ||||||||||
| ms.topic: reference | ||||||||||
| title: AvoidExclaimOperator | ||||||||||
| --- | ||||||||||
|
|
@@ -10,28 +10,32 @@ title: AvoidExclaimOperator | |||||||||
|
|
||||||||||
| ## Description | ||||||||||
|
|
||||||||||
| Avoid using the negation operator (`!`). Use `-not` for improved readability. | ||||||||||
| This rule detects the use of the negation operator exclamation mark (`!`) and recommends using the | ||||||||||
| `-not` operator instead for improved readability and consistency with PowerShell conventions. | ||||||||||
|
Comment on lines
+13
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| > [!NOTE] | ||||||||||
| > This rule is not enabled by default. The user needs to enable it through settings. | ||||||||||
| The `-not` operator is more explicit and aligns with PowerShell's verbose style, making code | ||||||||||
| easier to understand at a glance. | ||||||||||
|
|
||||||||||
| ## How to Fix | ||||||||||
| This rule is **disabled** by default. Enable it explicitly during ScriptAnalyzer invocation if | ||||||||||
| desired. | ||||||||||
|
|
||||||||||
| ## Example | ||||||||||
|
|
||||||||||
| ### Wrong | ||||||||||
| ### Noncompliant | ||||||||||
|
|
||||||||||
| ```powershell | ||||||||||
| $MyVar = !$true | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ### Correct | ||||||||||
| ### Compliant | ||||||||||
|
|
||||||||||
| ```powershell | ||||||||||
| $MyVar = -not $true | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Configuration | ||||||||||
| ## Configure rule | ||||||||||
|
|
||||||||||
| To enable this rule, run the following command: | ||||||||||
|
Xelu86 marked this conversation as resolved.
|
||||||||||
|
|
||||||||||
| ```powershell | ||||||||||
| Rules = @{ | ||||||||||
|
|
@@ -41,8 +45,12 @@ Rules = @{ | |||||||||
| } | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ### Parameters | ||||||||||
| ## Parameters | ||||||||||
|
|
||||||||||
| ### Enable | ||||||||||
|
|
||||||||||
| Enables (`$true`) the rule during ScriptAnalyzer invocation. | ||||||||||
|
|
||||||||||
| - `Enable`: **bool** (Default value is `$false`) | ||||||||||
| ### Disable | ||||||||||
|
|
||||||||||
| Enable or disable the rule during ScriptAnalyzer invocation. | ||||||||||
| Disables (`$false`) the rule during ScriptAnalyzer invocation. Default value is `$false`. | ||||||||||
|
Xelu86 marked this conversation as resolved.
Comment on lines
+48
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On reflection, and checking 396, which Sean reviewed and merged, this construction does seem a little strange. Do we have a template for the new document structure to use for rules? This construction implies that I can set either The previous construction shows a single configurable option ( |
||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||||
| --- | ||||||||||||
| description: Avoid global aliases. | ||||||||||||
| ms.date: 06/28/2023 | ||||||||||||
| ms.date: 05/28/2026 | ||||||||||||
| ms.topic: reference | ||||||||||||
| title: AvoidGlobalAliases | ||||||||||||
| --- | ||||||||||||
|
|
@@ -10,28 +10,29 @@ title: AvoidGlobalAliases | |||||||||||
|
|
||||||||||||
| ## Description | ||||||||||||
|
|
||||||||||||
| Globally scoped aliases override existing aliases within the sessions with matching names. This name | ||||||||||||
| collision can cause difficult to debug issues for consumers of modules and scripts. | ||||||||||||
| Global aliases can override existing aliases in the current session. This override | ||||||||||||
| creates naming conflicts that lead to hard to diagnose problems for module and script consumers. | ||||||||||||
|
Comment on lines
+13
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend updating this to use the same construction as
Suggested change
|
||||||||||||
|
|
||||||||||||
| To understand more about scoping, see `Get-Help about_Scopes`. | ||||||||||||
|
|
||||||||||||
| **NOTE** This rule is not available in PowerShell version 3 or 4 because it uses the | ||||||||||||
| This rule is not available in PowerShell version 3 or 4 because it uses the | ||||||||||||
| `StaticParameterBinder.BindCommand` API. | ||||||||||||
|
|
||||||||||||
| ## How | ||||||||||||
|
|
||||||||||||
| Use other scope modifiers for new aliases. | ||||||||||||
| Instead of using the Global scope, use other scope modifiers such as `Local` or `Process` when | ||||||||||||
| creating new aliases. To learn more, see [about_Scopes][01]. | ||||||||||||
|
Comment on lines
+19
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| ## Example | ||||||||||||
|
|
||||||||||||
| ### Wrong | ||||||||||||
| ### Noncompliant | ||||||||||||
|
|
||||||||||||
| ```powershell | ||||||||||||
| New-Alias -Name Name -Value Value -Scope Global | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### Correct | ||||||||||||
| ### Compliant | ||||||||||||
|
|
||||||||||||
| ```powershell | ||||||||||||
| New-Alias -Name Name1 -Value Value | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| <!-- link references --> | ||||||||||||
|
|
||||||||||||
| [01]: /powershell/module/microsoft.powershell.core/about/about_scopes | ||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,6 @@ | ||||||||||
| --- | ||||||||||
| description: No Global Variables | ||||||||||
| ms.date: 06/28/2023 | ||||||||||
| ms.date: 05/28/2026 | ||||||||||
| ms.topic: reference | ||||||||||
| title: AvoidGlobalVars | ||||||||||
| --- | ||||||||||
|
|
@@ -20,15 +20,12 @@ Globally scoped variables include: | |||||||||
| - Preference variables | ||||||||||
| - Variables, aliases, and functions that are in your PowerShell profiles | ||||||||||
|
|
||||||||||
| To understand more about scoping, see `Get-Help about_Scopes`. | ||||||||||
|
|
||||||||||
| ## How | ||||||||||
|
|
||||||||||
| Use other scope modifiers for variables. | ||||||||||
| Use local or script scope for variables instead of global scope. To learn more, see | ||||||||||
| [about_Scopes][01]. | ||||||||||
|
Comment on lines
+23
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ## Example | ||||||||||
|
|
||||||||||
| ### Wrong | ||||||||||
| ### Noncompliant | ||||||||||
|
|
||||||||||
| ```powershell | ||||||||||
| $Global:var1 = $null | ||||||||||
|
|
@@ -38,7 +35,7 @@ function Test-NotGlobal ($var) | |||||||||
| } | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ### Correct | ||||||||||
| ### Compliant | ||||||||||
|
|
||||||||||
| ```powershell | ||||||||||
| $var1 = $null | ||||||||||
|
|
@@ -47,3 +44,7 @@ function Test-NotGlobal ($var1, $var2) | |||||||||
| $a = $var1 + $var2 | ||||||||||
| } | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| <!-- link references --> | ||||||||||
|
|
||||||||||
| [01]: /powershell/module/microsoft.powershell.core/about/about_scopes | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.