PSScriptAnalyzer (PSSA) is an open-source tool that Microsoft developed. It is a static code checker for PowerShell modules and scripts. It checks the quality of the PowerShell code by running against some set of rules that checks for potential code defects in the scripts by applying a group of built-in or customized rules on the scripts being analyzed. Here is a similar article on PowerShell debugging, Ansible Syntax Checker, how to debug a PowerShell script, how to install and debug logs with the CMTrace Tool, how to perform PowerShell syntax check using PSScriptAnalyzer, and Script debugging: Difference between testing and debugging.
The default PSSA rules cover a wide variety of best practices, it checks your script, and warn you about using aliases instead of the full name of a command, using Invoke-Expression and other dangerous cmdlets, naming your functions correctly, and a whole lot more.
Note: You will have to set the execution policy. See these links for more information.
– How to set the Execution Policy via Windows Settings
– How to set the PowerShell Execution Policy.
– How to set PowerShell Execution Policy via the Registry
Now, let’s install the PSScriptAnalyzer Module: The minimum PowerShell version you should be running is 3.0. See the link for more information. Install using any of the cmdlets below.
Install-Module -Name PSScriptAnalyzer -RequiredVersion 1.11.0
Install-Module -Name PSScriptAnalyzer -Scope CurrentUser
You will be required to accept that you are okay with the installation of the PSScriptAnalyzer from an untrusted source “PSGallery”.
The PowerShell Gallery is the central repository for PowerShell content. In it, you can find useful PowerShell modules containing PowerShell commands and Desired State Configuration (DSC) resources. You can also find PowerShell scripts, some of which may contain PowerShell workflows, and which outline a set of tasks and provide sequencing for those tasks. Some of these packages are authored by Microsoft, and others are authored by the PowerShell community.
To verify this has been installed correctly, run the command below
Get-Module -Name PSScriptAnalyzer -ListAvailable
Note: Next, you will have to import the module in order to be able to use it. If you fail to import the module as shown below, you will get an error saying the command was found but the module could not be loaded.
Import-Module -Name PSScriptAnalyzer
To test my script, I can run the “Invoke-ScriptAnalyzer“. The script takes a path to a directory and gets all the items that have been written more than the specified number of days ago. You might be able to tell that there are no problems with this script already, just by looking at it. I have my random script saved as “MyScript.ps1“. To test, you can download any random script from this link.
Invoke-ScriptAnalyzer -Path .\MyScript.ps1
PSSA even tells you the line of your script where the violation occurs and provides a more detailed description of the rule violation.
Note: With the PSScriptAnalyzer installed, you could run your code through the Parser and observe if it raises an error.
I hope you found this blog post helpful. Please let me know in the comment session if you have any questions.