Getting Started with PowerShell Script Analyzer
Microsoft's powerful tool will make sure your scripts meet best practices.
- By Adam Bertram
As you get more experienced writing PowerShell script there comes a time when you're not only concerned about functionality but best practices and performance as well. Since PowerShell is so flexible, there are many ways to write a script, but that script may not always run the fastest or adhere to community best practices. This is why a handy PowerShell module called PSScriptAnalyzer was introduced by Microsoft. PSScriptAnalyzer is a free module that allows you to scan your scripts or modules for coding best practices.
In this article, we're going to cover what you can do with PSScriptAnalyzer, what is consists of and go over a couple examples of how to use it.
First of all, you'll need to install the PSScriptAnalyzer module as it is not available by default in any version of Windows at this time. The best way to get the module is to download it from the PowerShell Gallery. To do this requires PowerShellGet, which, in PowerShell v5, comes standard. Otherwise, you'll need to download PowerShellGet separately. Once you have PowerShellGet, you'll then have the Find and Install-Module cmdlets available to you.
To download PSScriptAnalzyer, open up a PowerShell console run as administrator and simply run Install-Module.
Install-Module -Name PSScriptAnalyzer
As of this writing, you will receive v1.5.0.
The PSScriptAnalyzer module only consists of two cmdlets; Get-ScriptAnalyzerRule and Invoke-ScriptAnalyzer. All of the magic happens in each individual rule that you can see by running Get-ScriptAnalyzerRule.
You can see when you run this, you'll get a list of various rules and what they check for. Just by reviewing this list will give you lots of tips to improve your scripts without even running PSScriptAnalyzer!
If you're happy with all of the default rules assigned, run Invoke-ScriptAnalyzer and pass it the path to the script or module you'd like to scan.
Invoke-ScriptAnalyzer -Path 'Script.ps1'
PSScriptAnalyzer will then parse the script and attempt to find violations of any of the default rules that you saw earlier. If you have multiple scripts in a folder, for example, you could also specify the folder and not the script itself. If you have scripts buried in other folders inside of that parent folder you can also use the Recurse parameter to search for scripts and modules to check.
Here's an example of some rule violations for a script I created long ago.
You can see that it shows you the rule that was violated, it's severity, the file name along with the line the violation was caught on and any specific information related to that rule violation.
Perhaps you'd like to test a snippet of code and not the entire script. For this, you can use the ScriptDefinition parameter and pass a string containing the code. Here's a good example of defining a function and passing it to PSScriptAnalyzer.
Perhaps you want to customize the default rules. You can do this by using the –IncludeRule or –ExcludeRule parameters. You can even create your own rules by using the CustomRulePath parameter.
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.