{"id":75971,"date":"2016-01-04T00:01:00","date_gmt":"2016-01-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2016\/01\/04\/a-favorite-powershell-ise-feature-script-analyzer\/"},"modified":"2019-02-18T09:20:25","modified_gmt":"2019-02-18T16:20:25","slug":"a-favorite-powershell-ise-feature-script-analyzer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-script-analyzer\/","title":{"rendered":"A Favorite PowerShell ISE Feature: Script Analyzer"},"content":{"rendered":"<p><b>Summary<\/b>: Sean Kearney introduces a free tool for examining your script.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here. This week I&rsquo;m going to introduce you to my favorite Windows PowerShell ISE features.<\/p>\n<p><b>&nbsp; &nbsp;Note<\/b>&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts about features in the Windows PowerShell ISE:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-script-analyzer\/\" target=\"_blank\">Script Analyzer<\/a><br \/>Download a free tool for examining your script<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-script-browser\/\" target=\"_blank\">Script Browser<\/a><br \/>Browse the TechNet Script Repository from the Windows PowerShell ISE<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-module-browser\/\" target=\"_blank\">Module Browser<\/a><br \/>Access modules in the PowerShell Gallery<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-snippets\/\" target=\"_blank\">Snippets<\/a><br \/>Get sample code in the Windows PowerShell&nbsp;ISE<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/a-favorite-powershell-ise-feature-remote-text-file-editing\/\" target=\"_blank\">Remote Text File Editing<\/a><br \/>Bring file editing to the Windows PowerShell&nbsp;ISE<\/li>\n<\/ul>\n<p>Today, I&rsquo;m talking about a free add-on called the Script Analyzer.<\/p>\n<p>In the past, I&rsquo;ve written some scripts that literally &ldquo;just worked,&rdquo; 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.<\/p>\n<p>&ldquo;What&rsquo;s the Script Analyzer?&rdquo; I asked myself as I scratched the few hairs on my head. &ldquo;I don&rsquo;t see a Script Analyzer anywhere.&rdquo;<\/p>\n<p>As it turns out, it&rsquo;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&rsquo;s also incredibly easy-to-use.<\/p>\n<p>To get this add-on, simply launch the PowerShell ISE, click <b>Add-ons<\/b>, and select <b>Get Script Analyzer Add-on Tool<\/b> from the menu.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0358.1.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0358.1.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>This will launch your browser to download and install the actual tool.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3060.2.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/3060.2.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>When you download and install this tool, you&rsquo;ll get not one, but two cool new features. The Script Analyzer and the Script Browser. I&rsquo;ll talk about the Script Browser tomorrow.<\/p>\n<p>When it is installed, you can access the Script Analyzer from the <b>Tools<\/b> menu on the right side of the ISE. If you don&rsquo;t see it, restart the PowerShell ISE and press <b>Alt<\/b>+<b>Shift<\/b>+<b>V<\/b> at least once to ensure the Add-on tools menu is displayed.<\/p>\n<p>It&rsquo;s also very easy-to-use. Select a script or module you&rsquo;re working on in the ISE. Here&rsquo;s one called Untitled as an example:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5543.3.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5543.3.PNG\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<p>I then click the <b>Scan Script<\/b> button and wait a few seconds (or maybe a minute). The time is dependent on the size and complexity of your script or module.<\/p>\n<p>&nbsp;<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4456.4.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4456.4.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>When the scan completes, it presents results and suggestions. This scan detected a couple minor issues:<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4214.5.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/4214.5.PNG\" alt=\"Image of results\" title=\"Image of results\" \/><\/a><\/p>\n<p>This does not mean your script is garbage or doesn&rsquo;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&rsquo;t explain what that error means. For that, I click the little gear icon to view the options.<\/p>\n<p>This brings up a menu of all the types of items that the Script Analyzer is looking for and why.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/67815.6.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/67815.6.PNG\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Clicking the rule presents a detailed explanation of why it&rsquo;s being trapped. So again, it&rsquo;s not an error so much as it&rsquo;s being flagged as &ldquo;not quite a best practice.&rdquo; 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.<\/p>\n<p>As you can see in the previous image, there is a link called <b>Suggest a new Script Analyzer Rule<\/b> that you can use if you see something it&rsquo;s not trapping you think it should be.<\/p>\n<p>If I click <b>Cancel<\/b> and go back to the script, I can edit the line in question.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/34048.7.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/34048.7.PNG\" alt=\"Image of script\" title=\"Image of script\" \/><\/a><\/p>\n<p>Running a new scan will either identify further issues or reveal that all is good.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2728.8.PNG\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2728.8.PNG\" alt=\"Image of message\" title=\"Image of message\" \/><\/a><\/p>\n<p>It&rsquo;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.<\/p>\n<p>Stop by tomorrow when I&rsquo;ll take a look at the other add-on we downloaded, the Script Browser in the ISE.<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy, Cloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Sean Kearney introduces a free tool for examining your script. Honorary Scripting Guy, Sean Kearney, is here. This week I&rsquo;m going to introduce you to my favorite Windows PowerShell ISE features. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts about features in the Windows PowerShell ISE: Script AnalyzerDownload a free tool [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,662,154,45],"class_list":["post-75971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-script-analyzer","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Sean Kearney introduces a free tool for examining your script. Honorary Scripting Guy, Sean Kearney, is here. This week I&rsquo;m going to introduce you to my favorite Windows PowerShell ISE features. &nbsp; &nbsp;Note&nbsp;&nbsp;&nbsp;This is a five-part series that includes the following posts about features in the Windows PowerShell ISE: Script AnalyzerDownload a free tool [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=75971"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/75971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=75971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=75971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=75971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}