Use PSScriptAnalyzer in PowerShell Studio
- Written by June Blender
- Last Updated: 14 June 2016
- Created: 11 June 2015
- Hits: 3569
- Windows PowerShell 3.0 and later
- PSScriptAnalyzer 1.1.0 and later
- PowerShell Studio 5.2.118 and later
PowerShell Studio has long supported best practices in Windows PowerShell, including running and debugging without profiles, avoiding cmdlet and parameter aliases (right-click\Expand to cmdlet, Expand all aliases: Ctrl+Shift+A), using consistent formatting to make maintenance and debugging easier, and using module-qualified command names (Ctrl+Shift+H).
Beginning in PowerShell Studio 5.2.118, we’ve added deep support for PSScriptAnalyzer, the static analysis tool designed especially for Windows PowerShell.
To use the Script Analyzer features in PowerShell Studio, you must be running in Windows PowerShell 3.0 and later and have the PSScriptAnalyzer module installed on the computer.
What is PSScriptAnalyzer?
PSScriptAnalyzer is a Microsoft-sponsored open-source Windows PowerShell module that evaluates scripts, modules, DSC resources (.ps1, .psm1, .psd1), commands, functions, and expressions (in strings) for compliance with a set of rules established by the PowerShell team and the community.
The rules vary in their severity (Error, Warning, Information), which is a measure of the importance of the rule violation. Many platforms that use PSScriptAnalyzer will not install or host modules that have PSScriptAnalyzer errors, although they might allow Warnings and Information messages.
You can specify the rules in each test, write custom rules, and suppress rules in your code.
The PSScriptAnalyzer module has only two cmdlets:
- Get-ScriptAnalyzerRule: Gets all or selected rules. You can use this cmdlet to learn about the rules and to select a set of rules use as the value of the IncludeRule or ExcludeRule parameters of Invoke-ScriptAnalyzer.
- Invoke-ScriptAnalyzer: Runs a PSScriptAnalyzer test on specified files or command strings.
Although PSScriptAnalyzer is not part of Windows PowerShell core, it’s so widely accepted that many people think of it as a core module.
Script Analyzer in PowerShell Studio
PowerShell Studio can run Script Analyzer automatically and on demand.
To run Script Analyzer on a file at any time:
- Click Tools and, in the Analysis group, click Analyze Script.
To run automatically:
- Whenever you run or debug a script, PowerShell Studio runs a Script Analyzer test on your script.
In either case, PowerShell Studio writes the results of the script analysis to the Debug tab of the Output Pane.
To avoid annoying you, the Debug tab doesn’t pop up. Instead, PowerShell Studio adds a reminder to the Output Pane. This reminder is not part of the output stream (stdout) and it does not affect the script results.
To use Script Analyzer results:
- In the Debug tab, click any rule violation. PowerShell Studio scrolls the Script tab to the line where the violation occurred so you can fix it.
Of course, you can turn the autorun feature on and off.
To enable and disable automatic script analysis:
- On the Home tab, in the Windows group, click Options.
- Click Editor and, in the Analysis section, click Automatically analyze scripts on run.
How to Get PSScriptAnalyzer
To use the script analyzer features of PowerShell Studio, install any valid version of the PSScriptAnalyzer module in a path listed in the value of your PSModulePath environment variable, $env:PSModulePath.
PSScriptAnalyzer is supported on Windows PowerShell 3.0 and later.
To install PSScriptAnalyzer:
Install the released version of PSScriptAnalyzer from PowerShell Gallery. To install scripts and modules from PowerShell Gallery, you need the PackageManagement and PowerShellGet modules. (PowerShellGet requires PackageManagement.)
- Windows PowerShell 5.0: The PackageManagement and PowerShellGet modules are included in Windows PowerShell 5.0 in Windows 10 and in Windows Management Framework 5.0.To install PSScriptAnalyzer (in $home\Documents\WindowsPowerShell\Modules):
Install-Module PSScriptAnalyzer -Scope CurrentUser
- Windows PowerShell 3.0 and 4.0:
- Download the latest version of the PackageManagement and PowerShellGet modules from the Microsoft Download Center.https://www.microsoft.com/en-us/download/details.aspx?id=51451 is the latest version at the time this blog post was published. Officially, it’s still a preview. Always search for later versions. They don’t always remember to remove the earlier ones.
- Use PowerShellGet to install PSScriptAnalyzer (in $home\Documents\WindowsPowerShell\Modules):
Install-Module PSScriptAnalyzer -Scope CurrentUser
You can also install an experimental version of PSScriptAnalyzer from GitHub: https://github.com/powershell/PSScriptAnalyzer. The PSScriptAnalyzer repository is a working site, so use this option only if you are an advanced user who is tolerant of in-progress versions.
If you have questions about PSScriptAnalyzer, ask them on our Windows PowerShell forum or use the Issues section of the PSScriptAnalyzer repository on GitHub. You don’t need to have any GitHub tools to read, search, open, or participate in an Issues discussion.