{"id":627,"date":"2014-09-25T00:01:00","date_gmt":"2014-09-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/25\/analyzing-powershell-script-files\/"},"modified":"2014-09-25T00:01:00","modified_gmt":"2014-09-25T00:01:00","slug":"analyzing-powershell-script-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/analyzing-powershell-script-files\/","title":{"rendered":"Analyzing PowerShell Script Files"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Learn how to analyze Windows PowerShell script files.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting around sipping a cup of English Breakfast tea. I added some peppermint, spearmint, lemon grass, licorice root, and a cinnamon stick to the pot. The result is a very refreshing cup of tea. I am sitting on the back porch with my Surface 3&nbsp;Pro and playing around with Windows PowerShell.<\/p>\n<p>I have a folder that I use to store miscellaneous Windows PowerShell scripts. I call the folder PSExtras and it contains scripts that I wrote for no apparent reason&mdash;I was just playing around. I decided that I would look at some of these scripts to determine a few things about the way I write scripts. To be fair, I have not added much to this folder in the last several years. Therefore, the folder is really more a picture of the way I used to write Windows PowerShell scripts three to four years ago.<\/p>\n<p>One of the cool things about the <b>Select-String<\/b> cmdlet is that not only will it parse a file, but it will also parse all files in a folder. For example, if I want to see the files where I used the <b>Write-Host<\/b> cmdlet, all I need to do is write something like:<\/p>\n<p style=\"margin-left:30px\">Select-String -Path E:\\Data\\PSExtras\\*.ps1 -Pattern &#039;write-host&#039; -List | select filename, line &ndash;Unique<\/p>\n<p>The output produces a list of the file names and the line where the <b>Write-Host<\/b> cmdlet appeared in the script.<\/p>\n<p>These days, the main reason I use the <b>Write-Host<\/b> cmdlet is because I want to display output in a different color. I always use the <b>&ndash;ForeGround<\/b> parameter when I do this. So, in how many of my scripts did I do this in my PSExtras folder? Here is the answer (this is a one-line command that is broken to fit on the website):<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Select-String -Path E:\\Data\\PSExtras\\*.ps1 -Pattern &#039;write-host -fore&#039;&nbsp; |<br \/><span style=\"font-size:12px\">select filename, line | sort filename -Unique | measure<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Count&nbsp;&nbsp;&nbsp; : 204<\/p>\n<p style=\"margin-left:30px\">Average&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Maximum&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Minimum&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Property :<\/p>\n<p>How many scripts did I write where I did not use the <b>&ndash;Foreground<\/b> parameter? I can easily get that by removing the <b>&ndash;fore<\/b> from my pattern. Here is the result:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Select-String -Path E:\\Data\\PSExtras\\*.ps1 -Pattern &#039;write-host&#039;&nbsp; |<br \/><span style=\"font-size:12px\">select filename, line | sort filename -Unique | measure<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Count&nbsp;&nbsp;&nbsp; : 375<\/p>\n<p style=\"margin-left:30px\">Average&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Maximum&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Minimum&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Property :<\/p>\n<p>But wait! That is all the files that include <b>Write-Host<\/b>. So the answer is 375-204, which is 171 scripts that did not include a directive to change the foreground color. Today I would not do that.<\/p>\n<p>What about the length of these scripts? I can get this information by using the <b>Measure-Object<\/b> cmdlet.<\/p>\n<p>To do this, I use the <b>Get-ChildItem<\/b> cmdlet to find all of my .ps1 script files. Then for each of them, I get the content of the file and pipe it to the <b>Measure-Object<\/b> cmdlet where I choose the <b>Line<\/b> property. Now I pipe that to the <b>Measure-Object<\/b> cmdlet, and I choose the <b>Average<\/b>, <b>Maximum<\/b>, and <b>Minimum<\/b> properties. Here is the command (using aliases) and the output from the command:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Gci E:\\Data\\PSExtras\\*.ps1 | % {get-content $_.fullname | measure -Line } | <br \/>measure -Property lines -Average -Maximum -Minimum<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Count&nbsp;&nbsp;&nbsp; : 1347<\/p>\n<p style=\"margin-left:30px\">Average&nbsp; : 35.7995545657016<\/p>\n<p style=\"margin-left:30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Maximum&nbsp; : 698<\/p>\n<p style=\"margin-left:30px\">Minimum&nbsp; : 1<\/p>\n<p style=\"margin-left:30px\">Property : Lines<\/p>\n<p>This tells me that I have 1347 scripts in my PSExtras folder, and that the average length is 35 lines (this also includes comments and blank lines). The maximum length of my scripts is 698 lines, and the shortest one (no surprise) is a one-liner.<\/p>\n<p>Most of the time, I do not save one-liners in files because I can easily re-create them (this is usually quicker than it takes me to actually locate the script file that contains the command), so this is not necessarily representative of my Windows PowerShell usage. But, I do think it is kind of interesting.<\/p>\n<p>How does this compare with the VBScript files I wrote over the years? I also have a VBSExtras folder, so let&#039;s find out:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Gci E:\\Data\\VBSExtras\\*.vbs | % {get-content $_.fullname | measure -Line } | <br \/>measure -Property lines -Average -Maximum -Minimum<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Count&nbsp;&nbsp;&nbsp; : 1610<\/p>\n<p style=\"margin-left:30px\">Average&nbsp; : 48.2391304347826<\/p>\n<p style=\"margin-left:30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Maximum&nbsp; : 1846<\/p>\n<p style=\"margin-left:30px\">Minimum&nbsp; : 0<\/p>\n<p style=\"margin-left:30px\">Property : Lines<\/p>\n<p>Wow! The average file length was 48 lines long, and my longest VBScript is 1846 lines long. Note that I also have more VBScript files than I have Windows PowerShell files in my *Extras folder. This is probably because of the fact that I do not save all my one-liners.<\/p>\n<p>Here is an image that shows the two commands and their associated output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-25-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-25-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using <b>Select-String<\/b> and working with files. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me 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 me 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, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to analyze Windows PowerShell script files. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting around sipping a cup of English Breakfast tea. I added some peppermint, spearmint, lemon grass, licorice root, and a cinnamon stick to the pot. The result is a very refreshing cup of tea. I [&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":[51,3,4,336,45],"class_list":["post-627","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-strings","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to analyze Windows PowerShell script files. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting around sipping a cup of English Breakfast tea. I added some peppermint, spearmint, lemon grass, licorice root, and a cinnamon stick to the pot. The result is a very refreshing cup of tea. I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/627","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=627"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/627\/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=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}