PowerShell Aliases, PSScriptAnalyzer and VSCode

Viewed 1350

Is there a way to suppress PSScriptAnalyzer from highlighting alias warnings? e.g.

'rm' is an alias of 'Remove-Item'. Aliases can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.

Aliases in PowerShell are extremely useful. I have a simple rule: I only ever use the rational built-in aliases in scripts (I ignore the strange ones). Why? Well, most of these particular aliases are now 13 years old and have never changed (PowerShell 1.0 release November 14, 2006). So, for example, % or ls or cd are reliable in 99.99% of cases. I consider 99.99% reliability to be "good enough". Possibly the single-most-over-repeated comment on all PowerShell StackOverflow questions is "Note: it is not recommended to use aliases in PowerShell scripts as they can change!" (not recommended by whom I often wonder? God? ;-) )

However, PSScriptAnalyzer in VSCode highlights all aliases as problems so that my current 7,000 line script has 488 such "problems". Is there a way to tell PSScriptAnalyzer that I like aliases, I intend to use aliases for the vastly more concise code, clarity, and greatly improved readability that they give me, and so I do not consider them to be problems?

1 Answers

Mathias' comment states to search for "Select PSScriptAnalyzer Rules" but I was not able to find that setting (VS 1.58.2, ms-vscode.powershell 2021.6.2).

The solution I found was to change the "Script Analysis: Settings Path" to point to a created file that contains the following code1 to whitelist certain aliases. Below I've un-commented the relevant section.

@{
# Only diagnostic records of the specified severity will be generated.
# Uncomment the following line if you only want Errors and Warnings but
# not Information diagnostic records.
#Severity = @('Error','Warning')

# Analyze **only** the following rules. Use IncludeRules when you want
# to invoke only a small subset of the default rules.
IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
                  'PSMisleadingBacktick',
                  'PSMissingModuleManifestField',
                  'PSReservedCmdletChar',
                  'PSReservedParams',
                  'PSShouldProcess',
                  'PSUseApprovedVerbs',
                  'PSAvoidUsingCmdletAliases',
                  'PSUseDeclaredVarsMoreThanAssignments')

# Do not analyze the following rules. Use ExcludeRules when you have
# commented out the IncludeRules settings above and want to include all
# the default rules except for those you exclude below.
# Note: if a rule is in both IncludeRules and ExcludeRules, the rule
# will be excluded.
#ExcludeRules = @('PSAvoidUsingWriteHost')

# You can use rule configuration to configure rules that support it:
Rules = @{
    PSAvoidUsingCmdletAliases = @{
        Whitelist = @("cd")
    }
}
}

[1] https://github.com/PowerShell/vscode-powershell/blob/master/examples/PSScriptAnalyzerSettings.psd1

Related