I use Visual Studio Code for writing PowerShell scripts, which makes formatting and writing so much easier. Even though it shows you things that you shouldn’t do, for example using aliases, you can get even more tips and hints to make your code even better. This blog post will show you how to use the PSScriptAnalyzer module which can give you some additional hints that Visual Studio Code doesn’t show you.
What is PSScriptAnalyzer?
“PSScriptAnalyzer is a static code checker for PowerShell modules and scripts. PSScriptAnalyzer checks the quality of PowerShell code by running a set of rules. The rules are based on PowerShell best practices identified by PowerShell Team and the community. It generates DiagnosticResults (errors and warnings) to inform users about potential code defects and suggests possible solutions for improvements.
PSScriptAnalyzer ships with a collection of built-in rules that check various aspects of PowerShell code such as:
- The presence of uninitialized variables
- Use of PSCredential type
- Use of
Invoke-Expression
- And many more”
Installing PSScriptAnalyzer
You can install the latest version by running:
Install-Module -Name PSScriptAnalyzer -Force
Using PSScriptAnalyzer
To check the quality of a script, you can use this:
Invoke-ScriptAnalyzer -Recurse D:\scripts\Set-CorrectHyperVExternalSwitchAdapter.ps1
In this example, it checks the Set-CorrectHyperVExternalSwitchAdapter.ps1 script and outputs the advice on screen:

In this case, it found two items. It advises you to change Write-Host to Write-Output and that the function should have SupportsShouldProcess included. Write-Output doesn’t give me the ForegroundColor that I use to color code the output, so I’ll ignore that 😉 But by adding SupportsShouldProcess to the function, for more information see here, the function gets the -WhatIf parameter which makes things safe if you are not sure what the function will do. (You do need to add code to handle that)
Below is another example, I used Invoke-ScriptAnalyzer against my New-MOTD script and it showed me this:

“‘ConvertFrom-HTMLClass’ is an alias of ‘ConvertFrom-HTMLAttributes’. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.”
That’s something that Visual Studio Code didn’t discover 🙂
Advanced PSScriptAnalayzer things
If you want to add custom rules, you can do so by following https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/create-custom-rule?view=ps-modules . You can specify to use any custom rule by specifying the CustomizedRulePath parameter
Rules Reference
You can read about all the default rules and what to do if it finds something in your script here, it’s a nice list and has a good explanation for the things that the script finds.
More information
You can find everything about the module here
You mention VSCode and PSScriptAnalyzer… do you have VSCode configured for PSSA to use a PSD1 file in VSCode?
No, just from the Terminal 🙂
Understood. There is a setting for PSScriptAnalyzer in VSCode, powershell.scriptanalysis.settingspath, allows you to point to a psd1. I like this for custom rules and code formatting customization. The CodeFormatting*.psd1 files here https://github.com/PowerShell/PSScriptAnalyzer/tree/master/Engine/Settings are a good start.
Ah, ok… Nice, the GitHub page is a good start but it’s not updated that much?!
No it is not updated often, I think at this point it is pretty stable.
Formatting is formatting of course, things don’t change that much and cmdlets/functions etc. still work the same throughout the versions of PowerShell. I will give it a try!