5

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?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • "have never changed" - this is not the case. A number of aliases have been removed in recent releases – Mathias R. Jessen Mar 27 '20 at 14:30
  • 1
    I can believe that Mathias. However, the aliases that I use *are* of the "have never changed" variety. e.g. `%`, `ls`, `cd` etc. I have never been one for using the more *exotic* (and frankly *weird*) aliases like `rvpa` (Resolve-Path), `rwmi` (Remove-WmiObject), `sajb` (Start-Job) etc. I use only those that are ultra-reliable - and by ultra-reliable, I mean "have never changed in 13 years" and "the kind of commands that I use very often every time I use PowerShell" (which `rvpa` never falls into the category of). – YorSubs Mar 27 '20 at 14:46
  • 1
    `sc -> Set-Content` works in 5.1, was removed in 6.2 `¯\_(ツ)_/¯` regardless: yes, you can suppress individual rules. Open the Command Palette (Shift+Ctrl+P), search for "Select PSScriptAnalyzer Rules" and uncheck `PSAvoidUsingCmdletAliases` – Mathias R. Jessen Mar 27 '20 at 14:47
  • `sc` is one that I have never used in any script, but good one (I'm guessing that they removed this as it might conflict with a linux command? I won't be having that problem in using `ls` or `mv` then! `:-p` ). Thanks Mathias, I'll go and look for these rules now! – YorSubs Mar 27 '20 at 14:51
  • 2
    if you REALLY want to make your code non-portable, difficult to read, AND just freaking ugly [*grin*] ... PSScriptAnalyzer has options to disable various tests - and can do so on a per script/line/everything basis. the instructions are on the PSSA site. – Lee_Dailey Mar 27 '20 at 15:11
  • I just want to suppress alias nagging really, but that's excellent thanks, I'll look it up :). The aliases I use make my code highly portable, easy to read and much better looking than those folks that create unreadable swamps of `ForEach-Object`, `GetChild-Item` (instead of `ls`), `Remove-Item` (instead of `rm`) everywhere. A philosophical question: *why is `rm` in Linux NOT freaking ugly, but is in PowerShell?* ... Another philosophical question: *why do people NOT feel the need to say "Note: it is not recommended to use functions in scripts as function names can change!"* hmm... lol :-D – YorSubs Mar 27 '20 at 15:21
  • Pretty sure you'd be in a minority about thinking your code is more portable OR readable using aliases. There are many people who would find such code indecipherable. – Scepticalist Mar 27 '20 at 15:46
  • 7
    I can say with *absolute* certainty that "indecipherable" is not true. e.g. `dir D:\ | more`. I bet more than a million dollars that you would be *extremely* hard-pressed to find even a single person with rudimentary programming ability that would find this "indecipherable". I get that you are against aliases, I just find the almost religious adherence to never using aliases (and the almost *resentment* projected at anyone that dares too!) to be slightly ... *bewildering*. I completely defend/support *your* right to not use aliases, I just find it somewhat "rigid". :-) – YorSubs Mar 27 '20 at 15:59

1 Answers1

1

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

edward2k6
  • 11
  • 1
  • 1