How to disable PSScriptAnalyzer's PSAvoidUsingCmdletAliases in PowerShell Extension in Visual Studio Code

5,160

There's three ways to do that.

Option 1 - Use the search function

  1. Hit F1 in Visual Studio Code to make the search bar appear
  2. Write >PowerShell: Select PS then choose PowerShell: Select PSScriptAnalyzer Rules
  3. remove the checkmark on PSAvoidUsingCmdletAliases
  4. Click on Confirm

Picture:

enter image description here

Option 2 - completely disable ScriptAnalysis

  1. Click the gear Icon in the bottom left corner in Visual Studio Code
  2. Click on Settings
  3. Click the {} Symbol on the top right corner
  4. Add "powershell.scriptAnalysis.enable": false to your user settings on the right hand side (see screenshot below).
  5. Save your User Settings by hitting CTRL + S

Screenshot:

enter image description here

Your Script Analyzer is now disabled and won't complain about Aliases anymore.

Option 3 - create a settings file and only disable Alias information

  1. Create a .psd1 File on your Filesystem. Copy the template from below into this file and save it.
  2. Go to your UserSettings in VSCode as described in Option 2 point 1 to 3.
  3. Add "powershell.scriptAnalysis.settingsPath": "C:\\foo\\bar\\FileName.psd1" and save it

Here's a picture of it:

enter image description here

Template (taken from https://github.com/PowerShell/vscode-powershell/blob/develop/examples/PSScriptAnalyzerSettings.psd1):

@{
    # 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 defualt rules.
    IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
                     'PSMisleadingBacktick',
                     'PSMissingModuleManifestField',
                     'PSReservedCmdletChar',
                     'PSReservedParams',
                     'PSShouldProcess',
                     'PSUseApprovedVerbs',
                     'PSUseDeclaredVarsMoreThanAssigments')

    # 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 = @('PSAvoidUsingAliases','PSAvoidUsingWriteHost')
}
Share:
5,160
SimonS
Author by

SimonS

usually (/◕ヮ◕)/ sometimes (ノಠ益ಠ)ノ彡┻━┻ PowerShell ❤

Updated on September 18, 2022

Comments

  • SimonS
    SimonS over 1 year

    I'm using Visual Studio Code to write my PowerShell Scripts.

    I've installed the ms-vscode.powershell PowerShell Extension for Visual Studio Code.

    Whenever I use an Alias in my Script, the PSScriptAnalyzer tells me to use the full CmdLet Name. This is kind of annoying because it also marks all aliases with a green curvy line.

    How can I disable this?

    see_screenshot_of_the_problem

    • Admin
      Admin over 5 years
      This is done because the best practice is not to use aliases in product scripts. powertheshell.com/bp_script_alias It makes them hard to read, hard to maintain and prone to error. Aliases is really targeted for interactive command line stuff. Sure you can use aliases in VSCode as you develop, yet, they really should be replaced before going to production. Use the expand alias command - blogs.technet.microsoft.com/heyscriptingguy/2017/01/12/…
  • SimonS
    SimonS over 5 years
    @root you're welcome. I'm extremely excited about Visual Studio Code! This is a great alternative to PowerShell ISE.
  • root
    root over 5 years
    Same here. I have some qualms with VSC, though I think they're due to my lack of understanding of its rich feature set and configurabilty.
  • PSaul
    PSaul about 5 years
    These sort of work, but not 100%. #1 doesn't persist if you close and re-open VSCode. #2 completely disables ScriptAnalysis, which disables ALL rules - some of them are useful. And #3 works, but means you can no longer use method #1 to easily adjust rules. Has anyone found a method that is like #1 but PERSISTS when you close/re-open VSCode?
  • SimonS
    SimonS about 5 years
    @PSaul use option 3 if you want them to persist. You can easily adjust for one session by using option 1 if needed.
  • PSaul
    PSaul almost 5 years
    Thanks @SimonS - I did end up using Option #3 (pointing to the file in my Dropbox so it syncs across workstations). However if you DO use Option #3 you CAN'T use Option #1 at the same time, you receive an error. If you want to adjust you must edit the .psd1 file in Option #3 and then remember to change back. Hopefully they'll fix Option #1 to persist in the future.
  • mklement0
    mklement0 over 4 years
    @PSaul: Re the selections not getting persisted with option #1 (as of v2019.11.0): See this GitHub issue
  • Luiz Felipe
    Luiz Felipe almost 3 years
    Does that tool have comment pragmas ?
  • PotatoFarmer
    PotatoFarmer almost 3 years
    The Powershell Script analyzer has an open issue/proposal to migrate Settings into more standardized VS Code style (and hopefully stability). As of 2021-07 I wasn't able to get create a settings file approach to work as expected - rules wouldn't get excluded, schema/syntax was opaque, and seemingly custom rule file meant default rules dropped.