Using PSScriptAnalyzer to optimize your PowerShell scripts

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

6 thoughts on “Using PSScriptAnalyzer to optimize your PowerShell scripts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.