Skip to content

Commit b166135

Browse files
committed
Backport docs updates from docs repo
1 parent ae712f7 commit b166135

20 files changed

+217
-122
lines changed

docs/Cmdlets/Get-ScriptAnalyzerRule.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
external help file: Microsoft.Windows.PowerShell.ScriptAnalyzer.dll-Help.xml
33
Module Name: PSScriptAnalyzer
4-
ms.date: 10/07/2021
4+
ms.date: 12/12/2024
55
online version: https://learn.microsoft.com/powershell/module/psscriptanalyzer/get-scriptanalyzerrule?view=ps-modules&wt.mc_id=ps-gethelp
66
schema: 2.0.0
77
---
@@ -92,7 +92,7 @@ one value, but wildcards are supported. To get rules in subdirectories of the pa
9292
**RecurseCustomRulePath** parameter.
9393

9494
You can create custom rules using a .NET assembly or a PowerShell module, such as the
95-
[Community Analyzer Rules](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1)
95+
[Community Analyzer Rules](https://github.com/PowerShell/PSScriptAnalyzer/tree/main/Tests/Engine/CommunityAnalyzerRules)
9696
in the GitHub repository.
9797

9898
```yaml

docs/Cmdlets/Invoke-ScriptAnalyzer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ value of the **Profile** parameter is the path to the Script Analyzer profile.
192192
ExcludeRules = '*WriteHost'
193193
}
194194
195-
Invoke-ScriptAnalyzer -Path $pshome\Modules\BitLocker -Profile .\ScriptAnalyzerProfile.txt
195+
Invoke-ScriptAnalyzer -Path $pshome\Modules\BitLocker -Settings .\ScriptAnalyzerProfile.txt
196196
```
197197

198198
If you include a conflicting parameter in the `Invoke-ScriptAnalyzer` command, such as

docs/Rules/AvoidDefaultValueSwitchParameter.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Switch Parameters Should Not Default To True
3-
ms.date: 06/28/2023
3+
ms.date: 12/05/2024
44
ms.topic: reference
55
title: AvoidDefaultValueSwitchParameter
66
---
@@ -10,11 +10,19 @@ title: AvoidDefaultValueSwitchParameter
1010

1111
## Description
1212

13-
Switch parameters for commands should default to false.
13+
If your parameter takes only `true` and `false`, define the parameter as type `[Switch]`. PowerShell
14+
treats a switch parameter as `true` when it's used with a command. If the parameter isn't included
15+
with the command, PowerShell considers the parameter to be false. Don't define `[Boolean]`
16+
parameters.
17+
18+
You shouldn't define a switch parameter with a default value of `$true` because this isn't the
19+
expected behavior of a switch parameter.
1420

1521
## How
1622

17-
Change the default value of the switch parameter to be false.
23+
Change the default value of the switch parameter to be `$false` or don't provide a default value.
24+
Write the logic of the script to assume that the switch parameter default value is `$false` or not
25+
provided.
1826

1927
## Example
2028

@@ -48,8 +56,22 @@ function Test-Script
4856
$Param1,
4957
5058
[switch]
51-
$Switch=$False
59+
$Switch
5260
)
61+
62+
begin {
63+
# Ensure that the $Switch is set to false if not provided
64+
if (-not $PSBoundParameters.ContainsKey('Switch')) {
65+
$Switch = $false
66+
}
67+
}
5368
...
5469
}
5570
```
71+
72+
## More information
73+
74+
- [Strongly Encouraged Development Guidelines][01]
75+
76+
<!-- link references -->
77+
[01]: https://learn.microsoft.com/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines#parameters-that-take-true-and-false

docs/Rules/AvoidGlobalFunctions.md

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ title: AvoidGlobalFunctions
1313
Globally scoped functions override existing functions within the sessions with matching names. This
1414
name collision can cause difficult to debug issues for consumers of modules.
1515

16-
1716
To understand more about scoping, see `Get-Help about_Scopes`.
1817

1918
## How

docs/Rules/AvoidOverwritingBuiltInCmdlets.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Avoid overwriting built in cmdlets
3-
ms.date: 06/28/2023
3+
ms.date: 12/12/2024
44
ms.topic: reference
55
title: AvoidOverwritingBuiltInCmdlets
66
---
@@ -14,7 +14,7 @@ This rule flags cmdlets that are available in a given edition/version of PowerSh
1414
operating system which are overwritten by a function declaration. It works by comparing function
1515
declarations against a set of allowlists that ship with PSScriptAnalyzer. These allowlist files are
1616
used by other PSScriptAnalyzer rules. More information can be found in the documentation for the
17-
[UseCompatibleCmdlets](./UseCompatibleCmdlets.md) rule.
17+
[UseCompatibleCmdlets][01] rule.
1818

1919
## Configuration
2020

@@ -37,14 +37,17 @@ following your settings file.
3737

3838
The parameter `PowerShellVersion` is a list of allowlists that ship with PSScriptAnalyzer.
3939

40-
**Note**: The default value for `PowerShellVersion` is `core-6.1.0-windows` if PowerShell 6 or
41-
later is installed, and `desktop-5.1.14393.206-windows` if it is not.
40+
> [!NOTE]
41+
> The default value for `PowerShellVersion` is `core-6.1.0-windows` if PowerShell 6 or
42+
> later is installed, and `desktop-5.1.14393.206-windows` if it's not.
4243
4344
Usually, patched versions of PowerShell have the same cmdlet data, therefore only settings of major
4445
and minor versions of PowerShell are supplied. One can also create a custom settings file as well
45-
with the
46-
[New-CommandDataFile.ps1](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Utils/New-CommandDataFile.ps1)
47-
script and use it by placing the created `JSON` into the `Settings` folder of the `PSScriptAnalyzer`
48-
module installation folder, then the `PowerShellVersion` parameter is just its file name (that can
49-
also be changed if desired). Note that the `core-6.0.2-*` files were removed in PSScriptAnalyzer
50-
1.18 since PowerShell 6.0 reached end of life.
46+
with the [New-CommandDataFile.ps1][02] script and use it by placing the created `JSON` into the
47+
`Settings` folder of the `PSScriptAnalyzer` module installation folder, then the `PowerShellVersion`
48+
parameter is just its filename (that can also be changed if desired). Note that the `core-6.0.2-*`
49+
files were removed in PSScriptAnalyzer 1.18 since PowerShell 6.0 reached end of life.
50+
51+
<!-- link references -->
52+
[01]: ./UseCompatibleCmdlets.md
53+
[02]: https://github.com/PowerShell/PSScriptAnalyzer/blob/main/Utils/New-CommandDataFile.ps1

docs/Rules/AvoidUsingCmdletAliases.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ There are also implicit aliases. When PowerShell cannot find the cmdlet name, it
2020
Every PowerShell author learns the actual command names, but different authors learn and use
2121
different aliases. Aliases can make code difficult to read, understand and impact availability.
2222

23-
Using the full command name makes it eaiser to maintain your scripts in the the future.
23+
Using the full command name makes it easier to maintain your scripts in the the future.
2424

2525
Using the full command names also allows for syntax highlighting in sites and applications like
2626
GitHub and Visual Studio Code.

docs/Rules/AvoidUsingConvertToSecureStringWithPlainText.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Avoid Using SecureString With Plain Text
3-
ms.date: 06/28/2023
3+
ms.date: 01/28/2025
44
ms.topic: reference
55
title: AvoidUsingConvertToSecureStringWithPlainText
66
---
@@ -37,6 +37,4 @@ $EncryptedInput = ConvertTo-SecureString -String $UserInput -AsPlainText -Force
3737

3838
```powershell
3939
$SecureUserInput = Read-Host 'Please enter your secure code' -AsSecureString
40-
$EncryptedInput = ConvertFrom-SecureString -String $SecureUserInput
41-
$SecureString = ConvertTo-SecureString -String $EncryptedInput
4240
```

docs/Rules/AvoidUsingWriteHost.md

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Avoid Using Write-Host
3-
ms.date: 06/28/2023
3+
ms.date: 12/05/2024
44
ms.topic: reference
55
title: AvoidUsingWriteHost
66
---
@@ -10,10 +10,15 @@ title: AvoidUsingWriteHost
1010

1111
## Description
1212

13-
The use of `Write-Host` is greatly discouraged unless in the use of commands with the `Show` verb.
14-
The `Show` verb explicitly means 'show on the screen, with no other possibilities'.
13+
The primary purpose of the `Write-Host` cmdlet is to produce display-only output in the host. For
14+
example: printing colored text or prompting the user for input when combined with `Read-Host`.
15+
`Write-Host` uses the `ToString()` method to write the output. The particular result depends on the
16+
program that's hosting PowerShell. The output from `Write-Host` isn't sent to the pipeline. To
17+
output data to the pipeline, use `Write-Output` or implicit output.
1518

16-
Commands with the `Show` verb do not have this check applied.
19+
The use of `Write-Host` in a function is discouraged unless the function uses the `Show` verb. The
20+
`Show` verb explicitly means _display information to the user_. This rule doesn't apply to functions
21+
with the `Show` verb.
1722

1823
## How
1924

@@ -27,22 +32,22 @@ logging or returning one or more objects.
2732
```powershell
2833
function Get-MeaningOfLife
2934
{
30-
...
3135
Write-Host 'Computing the answer to the ultimate question of life, the universe and everything'
32-
...
3336
Write-Host 42
3437
}
3538
```
3639

3740
### Correct
3841

42+
Use `Write-Verbose` for informational messages. The user can decide whether to see the message by
43+
providing the **Verbose** parameter.
44+
3945
```powershell
4046
function Get-MeaningOfLife
4147
{
42-
[CmdletBinding()]Param() # to make it possible to set the VerbosePreference when calling the function
43-
...
48+
[CmdletBinding()]Param() # makes it possible to support Verbose output
49+
4450
Write-Verbose 'Computing the answer to the ultimate question of life, the universe and everything'
45-
...
4651
Write-Output 42
4752
}
4853
@@ -51,3 +56,7 @@ function Show-Something
5156
Write-Host 'show something on screen'
5257
}
5358
```
59+
60+
## More information
61+
62+
[Write-Host](xref:Microsoft.PowerShell.Utility.Write-Host)

docs/Rules/PlaceOpenBrace.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ Enforce a new line character after an open brace. The default value is true.
4545
#### IgnoreOneLineBlock: bool (Default value is `$true`)
4646

4747
Indicates if open braces in a one line block should be ignored or not. For example,
48-
` $x = if ($true) { 'blah' } else { 'blah blah' }`, if the property is set to true then the rule
48+
`$x = if ($true) { 'blah' } else { 'blah blah' }`, if the property is set to true then the rule
4949
doesn't fire a violation.

docs/Rules/PossibleIncorrectComparisonWithNull.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Null Comparison
3-
ms.date: 06/28/2023
3+
ms.date: 12/03/2024
44
ms.topic: reference
55
title: PossibleIncorrectComparisonWithNull
66
---
@@ -18,8 +18,8 @@ There are multiple reasons why this occurs:
1818
- `$null` is a scalar value. When the value on the left side of an operator is a scalar, comparison
1919
operators return a **Boolean** value. When the value is a collection, the comparison operators
2020
return any matching values or an empty array if there are no matches in the collection.
21-
- PowerShell performs type casting left to right, resulting in incorrect comparisons when `$null` is
22-
cast to other scalar types.
21+
- PowerShell performs type casting on the right-hand operand, resulting in incorrect comparisons
22+
when `$null` is cast to other scalar types.
2323

2424
The only way to reliably check if a value is `$null` is to place `$null` on the left side of the
2525
operator so that a scalar comparison is performed.
@@ -55,10 +55,10 @@ function Test-CompareWithNull
5555
## Try it Yourself
5656

5757
```powershell
58-
# Both expressions below return 'false' because the comparison does not return an
59-
# object and therefore the if statement always falls through:
58+
# This example returns 'false' because the comparison does not return any objects from the array
6059
if (@() -eq $null) { 'true' } else { 'false' }
61-
if (@() -ne $null) { 'true' } else { 'false' }
60+
# This example returns 'true' because the array is empty
61+
if ($null -ne @()) { 'true' } else { 'false' }
6262
```
6363

6464
This is how the comparison operator works by-design. But, as demonstrated, this can lead

docs/Rules/PossibleIncorrectUsageOfAssignmentOperator.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if ($a = Get-Something) # Only execute action if command returns something and a
5252
}
5353
```
5454

55-
## Implicit suppresion using Clang style
55+
## Implicit suppression using Clang style
5656

5757
There are some rare cases where assignment of variable inside an `if` statement is by design.
5858
Instead of suppressing the rule, one can also signal that assignment was intentional by wrapping the

docs/Rules/ProvideCommentHelp.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Rules = @{
3636

3737
### Parameters
3838

39-
- `Enable`: **bool** (Default valus is `$true`)
39+
- `Enable`: **bool** (Default value is `$true`)
4040

4141
Enable or disable the rule during ScriptAnalyzer invocation.
4242

docs/Rules/UseBOMForUnicodeEncodedFile.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Use BOM encoding for non-ASCII files
3-
ms.date: 06/28/2023
3+
ms.date: 01/07/2025
44
ms.topic: reference
55
title: UseBOMForUnicodeEncodedFile
66
---
@@ -13,6 +13,30 @@ title: UseBOMForUnicodeEncodedFile
1313
For a file encoded with a format other than ASCII, ensure Byte Order Mark (BOM) is present to ensure
1414
that any application consuming this file can interpret it correctly.
1515

16+
You can use this rule to test any arbitrary text file, but the intent is to ensure that PowerShell
17+
scripts are saved with a BOM when using a Unicode encoding.
18+
1619
## How
1720

18-
Ensure that the file is encoded with BOM present.
21+
For PowerShell commands that write to files, ensure that you set the encoding parameter to a value
22+
that produces a BOM. In PowerShell 7 and higher, the following values of the **Encoding** parameter
23+
produce a BOM:
24+
25+
- `bigendianunicode`
26+
- `bigendianutf32`
27+
- `oem`
28+
- `unicode`
29+
- `utf32`
30+
- `utf8BOM`
31+
32+
When you create a script file using a text editor, ensure that the editor is configured to save the
33+
file with a BOM. Consult the documentation for your text editor for instructions on how to save
34+
files with a BOM.
35+
36+
## Further reading
37+
38+
For more information, see the following articles:
39+
40+
- [about_Character_Encoding](/powershell/module/microsoft.powershell.core/about/about_character_encoding)
41+
- [Set-Content](xref:Microsoft.PowerShell.Management.Set-Content)
42+
- [Understanding file encoding in VS Code and PowerShell](/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding)

docs/Rules/UseCompatibleCmdlets.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Use compatible cmdlets
3-
ms.date: 06/28/2023
3+
ms.date: 12/12/2024
44
ms.topic: reference
55
title: UseCompatibleCmdlets
66
---
@@ -10,8 +10,8 @@ title: UseCompatibleCmdlets
1010

1111
## Description
1212

13-
This rule flags cmdlets that are not available in a given Edition/Version of PowerShell on a given
14-
Operating System. It works by comparing a cmdlet against a set of allowlists which ship with
13+
This rule flags cmdlets that aren't available in a given Edition and Version of PowerShell on a
14+
given Operating System. It works by comparing a cmdlet against a set of allowlists which ship with
1515
PSScriptAnalyzer. They can be found at `/path/to/PSScriptAnalyzerModule/Settings`. These files are
1616
of the form, `<psedition>-<psversion>-<os>.json` where `<psedition>` can be either `Core` or
1717
`Desktop`, `<os>` can be either `Windows`, `Linux` or `MacOS`, and `<psversion>` is the PowerShell
@@ -41,7 +41,10 @@ The parameter `compatibility` is a list that contain any of the following
4141

4242
Usually, patched versions of PowerShell have the same cmdlet data, therefore only settings of major
4343
and minor versions of PowerShell are supplied. You can also create a custom settings file with the
44-
[New-CommandDataFile.ps1](https://github.com/PowerShell/PSScriptAnalyzer/blob/development/Utils/New-CommandDataFile.ps1)
45-
script. Place the created `.json` file in the `Settings` folder of the `PSScriptAnalyzer` module
46-
folder. Then the `compatibility` parameter values is just the filename. Note that the `core-6.0.2-*`
47-
files were removed in PSScriptAnalyzer 1.18 since PowerShell 6.0 reached it's end of life.
44+
[New-CommandDataFile.ps1][01] script. Place the created `.json` file in the `Settings` folder of the
45+
`PSScriptAnalyzer` module folder. Then the `compatibility` parameter values is just the filename.
46+
Note that the `core-6.0.2-*` files were removed in PSScriptAnalyzer 1.18 since PowerShell 6.0
47+
reached it's end of life.
48+
49+
<!-- link references -->
50+
[01]: https://github.com/PowerShell/PSScriptAnalyzer/blob/main/Utils/New-CommandDataFile.ps1

0 commit comments

Comments
 (0)