ScriptAnalyzer ships with sets of built-in preset rules that can be used to analyze scripts. These include: PSGallery
, DSC
and CodeFormatting
. They can be executed as follows:
PowerShell Gallery rules
To execute the PowerShell Gallery rules use the following command:
Invoke-ScriptAnalyzer -Path /path/to/module/ -Settings PSGallery -Recurse
DSC rules
To execute the DSC rules use the following command:
Invoke-ScriptAnalyzer -Path /path/to/module/ -Settings DSC -Recurse
Code formatting rules
To execute the code formatting rules use the following command:
Invoke-ScriptAnalyzer -Path /path/to/module/ -Settings CodeFormatting -Recurse
To run the script analyzer against a single script file execute:
Invoke-ScriptAnalyzer -Path myscript.ps1
This will analyze your script against every built-in rule. If your script is sufficiently large that could result in a lot of warnings and/or errors.
To run the script analyzer against a whole directory, specify the folder containing the script, module and DSC files you want analyzed. Specify the Recurse parameter if you also want sub-directories searched for files to analyze.
Invoke-ScriptAnalyzer -Path . -Recurse
To see all the built-in rules execute:
Get-ScriptAnalyzerRule
Get-ScriptAnalyzerRule [-CustomizedRulePath <string[]>] [-Name <string[]>] [-Severity <string[]>] [<CommonParameters>]
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule<string[]>] [-Severity <string[]>] [-Recurse] [-SuppressedOnly] [<CommonParameters>]