Get-ScriptAnalyzerRule

Gets the script analyzer rules on the local computer.

Syntax

Get-ScriptAnalyzerRule
   [[-Name] <string[]>]
   [-CustomRulePath <string[]>]
   [-RecurseCustomRulePath]
   [-Severity <string[]>]
   [<CommonParameters>]

Description

Gets the script analyzer rules on the local computer. You can select rules by Name, Severity, Source, or SourceType, or even particular words in the rule description.

Use this cmdlet to create collections of rules to include and exclude when running the Invoke-ScriptAnalyzer cmdlet.

To get information about the rules, see the value of the Description property of each rule.

The PSScriptAnalyzer module tests the PowerShell code in a script, module, or DSC resource to determine if it fulfils best practice standards.

Examples

EXAMPLE 1 - Get all Script Analyzer rules on the local computer

Get-ScriptAnalyzerRule

EXAMPLE 2 - Gets only rules with the Error severity

Get-ScriptAnalyzerRule -Severity Error

EXAMPLE 3 - Run only the DSC rules with the Error severity

This example runs only the DSC rules with the Error severity on the files in the MyDSCModule module.

$DSCError = Get-ScriptAnalyzerRule -Severity Error | Where-Object SourceName -eq PSDSC
$Path = "$home\Documents\WindowsPowerShell\Modules\MyDSCModule\*"
Invoke-ScriptAnalyzerRule -Path $Path -IncludeRule $DSCError -Recurse

Using the IncludeRule parameter of Invoke-ScriptAnalyzerRule is more efficient than using its Severity parameter, which is applied only after using all rules to analyze all module files.

EXAMPLE 4 - Get rules by name and severity

This example gets rules with "Parameter" or "Alias" in the name that generate an Error or Warning. You can use this set of rules to test the parameters of your script or module.

$TestParameters = Get-ScriptAnalyzerRule -Severity Error, Warning -Name *Parameter*, *Alias*

EXAMPLE 5 - Get custom rules

This example gets the standard rules and the rules in the VeryStrictRules and ExtremelyStrictRules modules. The command uses the RecurseCustomRulePath parameter to get rules defined in subdirectories of the matching paths.

Get-ScriptAnalyzerRule -CustomRulePath $home\Documents\WindowsPowerShell\Modules\*StrictRules -RecurseCustomRulePath

Parameters

-CustomRulePath

By default, PSScriptAnalyzer gets only the standard rules specified in the Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.dll file in the module. Use this parameter to get the custom Script Analyzer rules in the specified path and the standard Script Analyzer rules.

Enter the path to a .NET assembly or module that contains Script Analyzer rules. You can enter only one value, but wildcards are supported. To get rules in subdirectories of the path, use the RecurseCustomRulePath parameter.

You can create custom rules using a .NET assembly or a PowerShell module, such as the Community Analyzer Rules in the GitHub repository.

Type:String[]
Aliases:CustomizedRulePath
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Name

Gets only rules with the specified names or name patterns. Wildcards are supported. If you list multiple names or patterns, it gets all rules that match any of the name patterns.

Type:String[]
Position:Named
Default value:All rules
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-RecurseCustomRulePath

Searches the CustomRulePath location recursively to add rules defined in files in subdirectories of the path. By default, Get-ScriptAnalyzerRule adds only the custom rules in the specified path.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Severity

Gets only rules with the specified severity values. Valid values are:

  • Information
  • Warning
  • Error
Type:String[]
Position:Named
Default value:All rules
Required:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

None

You can't pipe input to this cmdlet.

Outputs

Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.RuleInfo

The RuleInfo object is a custom object created specifically for Script Analyzer.