A Favorite PowerShell ISE Feature: Script Analyzer
Summary: Sean Kearney introduces a free tool for examining your script.
Honorary Scripting Guy, Sean Kearney, is here. This week I’m going to introduce you to my favorite Windows PowerShell ISE features.
Note This is a five-part series that includes the following posts about features in the Windows PowerShell ISE:
- Script Analyzer
Download a free tool for examining your script
- Script Browser
Browse the TechNet Script Repository from the Windows PowerShell ISE
- Module Browser
Access modules in the PowerShell Gallery
Get sample code in the Windows PowerShell ISE
- Remote Text File Editing
Bring file editing to the Windows PowerShell ISE
Today, I’m talking about a free add-on called the Script Analyzer.
In the past, I’ve written some scripts that literally “just worked,” but I still wondered if there was something I missed. More recently I wrote a couple of modules for the PowerShell Gallery. A requirement before uploading them was to run them through the Script Analyzer.
“What’s the Script Analyzer?” I asked myself as I scratched the few hairs on my head. “I don’t see a Script Analyzer anywhere.”
As it turns out, it’s a free add-on to the PowerShell ISE that allows you to scan through your script or module and get you as close to best practices as possible. It’s also incredibly easy-to-use.
To get this add-on, simply launch the PowerShell ISE, click Add-ons, and select Get Script Analyzer Add-on Tool from the menu.
This will launch your browser to download and install the actual tool.
When you download and install this tool, you’ll get not one, but two cool new features. The Script Analyzer and the Script Browser. I’ll talk about the Script Browser tomorrow.
When it is installed, you can access the Script Analyzer from the Tools menu on the right side of the ISE. If you don’t see it, restart the PowerShell ISE and press Alt+Shift+V at least once to ensure the Add-on tools menu is displayed.
It’s also very easy-to-use. Select a script or module you’re working on in the ISE. Here’s one called Untitled as an example:
I then click the Scan Script button and wait a few seconds (or maybe a minute). The time is dependent on the size and complexity of your script or module.
When the scan completes, it presents results and suggestions. This scan detected a couple minor issues:
This does not mean your script is garbage or doesn’t work. It is simply making suggestions based on best practices. I can immediately double-click a line number and it will take me to the issue in question. For a new user of the Script Analyzer, the problem is that it doesn’t explain what that error means. For that, I click the little gear icon to view the options.
This brings up a menu of all the types of items that the Script Analyzer is looking for and why.
Clicking the rule presents a detailed explanation of why it’s being trapped. So again, it’s not an error so much as it’s being flagged as “not quite a best practice.” If this was a vendor script that has been digitally signed and you wanted to ignore a particular rule, you can clear that check box and re-scan the script.
As you can see in the previous image, there is a link called Suggest a new Script Analyzer Rule that you can use if you see something it’s not trapping you think it should be.
If I click Cancel and go back to the script, I can edit the line in question.
Running a new scan will either identify further issues or reveal that all is good.
It’s not a perfect solution. There are many things it still misses (for example, unreferenced variables and lack of Help for cmdlets). But it is a decent and free solution from Microsoft to get you going in your scripting quest.
Stop by tomorrow when I’ll take a look at the other add-on we downloaded, the Script Browser in the ISE.
I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.
Sean Kearney, Honorary Scripting Guy, Cloud and Datacenter Management MVP