{"id":4042,"date":"2013-03-10T00:01:00","date_gmt":"2013-03-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/03\/10\/weekend-scripter-playing-around-with-powershell-modules\/"},"modified":"2013-03-10T00:01:00","modified_gmt":"2013-03-10T00:01:00","slug":"weekend-scripter-playing-around-with-powershell-modules","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-playing-around-with-powershell-modules\/","title":{"rendered":"Weekend Scripter: Playing Around with PowerShell Modules"},"content":{"rendered":"<p><strong>Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, talks about discovering cool information by playing around with Windows PowerShell modules.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is the weekend here in Charlotte, North Carolina in the United States. I am sipping a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Oolong\" target=\"_blank\">oolong<\/a> green tea with a bit of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Jasmine\" target=\"_blank\">jasmine<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cinnamomum_verum\" target=\"_blank\">cinnamon<\/a> stick, and eating some fresh strawberries that the Scripting Wife found. I am listening to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Traffic_(band)\" target=\"_blank\">Traffic<\/a> on my laptop, I have my Windows PowerShell console open, and I am playing around with modules.<\/p>\n<h2>Modules that have nested modules<\/h2>\n<p>The first thing to do when playing with module information is to import all of the modules. The easy way to do this is to use <strong>Get-Module<\/strong> (<strong>gmo<\/strong> is an alias) to get the modules, and then pipe the module info to the <strong>Import-Module<\/strong> cmdlet (<strong>ipmo<\/strong> is an alias). This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">gmo -ListAvailable | ipmo<\/p>\n<p>After I have imported all of the modules, I can use the <strong>Get-Module<\/strong> cmdlet to find modules that have nested modules. To do this, I look for values in the <strong>NestedModules<\/strong><em> <\/em>property. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Module | ? nestedmodules | select name, nestedmodules<\/p>\n<p>The command and its associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.HSG-3-10-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7635.HSG-3-10-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To find out how many modules have nested modules, I pipe the results to the <strong>Measure-Object<\/strong> cmdlet as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Module | ? nestedmodules | select name, nestedmodules | measure<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 55<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<p>To see how many modules I have loaded, I use the <strong>Get-Module<\/strong> cmdlet and pipe the results to the <strong>Measure-Object<\/strong> cmdlet. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Module | measure<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 81<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<h2>Finding numbers of exported commands<\/h2>\n<p>To find the number of exported commands from a cmdlet, I need to count how many cmdlets exist in the <strong>ExportedCommands<\/strong><em> <\/em>property. I can directly access the <strong>ExportedCommands<\/strong><em> <\/em>property by grouping the results of the command. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Module bestpractices).exportedcommands<\/p>\n<p style=\"padding-left: 30px\">Key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Get-BpaModel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Get-BpaModel<\/p>\n<p style=\"padding-left: 30px\">Get-BpaResult &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Get-BpaResult<\/p>\n<p style=\"padding-left: 30px\">Invoke-BpaModel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Invoke-BpaModel<\/p>\n<p style=\"padding-left: 30px\">Set-BpaResult&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set-BpaResult<\/p>\n<p>With the <strong>BestPractices<\/strong> module, it is rather easy to see how many commands it exports. But with a module such as the <strong>ActiveDirectory<\/strong> module, it may not be quite so easy. I could pipe the results to the <strong>Measure-Object<\/strong> cmdlet&mdash;but because the <strong>ExportedCommands<\/strong><em> <\/em>property contains a dictionary object, it means I have access to the <strong>Count<\/strong><em> <\/em>property. Therefore, I can simply add <strong>Count<\/strong><em> <\/em>to the end of the command and arrive at the number of commands that are exported by the module. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Module bestpractices).exportedcommands.count<\/p>\n<p style=\"padding-left: 30px\">4<\/p>\n<p>OK, that is cool, but what I really need is the ability to obtain the module name and the number of commands it exports. To do this, I need to create a custom object. The easiest way to create a custom object is to use the <strong>Select-Object<\/strong> cmdlet and select the properties I need. While I am at it, I want to create a custom label. The following is the command that I arrive at.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Module bestpractices | select name, @{LABEL=&#8217;cmdletCount&#8217;;EXPRESSION={$_.exportedcommands.count}}<\/p>\n<p style=\"padding-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bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4<\/p>\n<p>Cool. I can now simply delete the <strong>BestPractices<\/strong><em> <\/em>module name, and I will obtain a list of all modules and the number of commands they export. Here is the revised code:<\/p>\n<p style=\"padding-left: 30px\">Get-Module | select name, @{LABEL=&#8217;cmdletCount&#8217;;EXPRESSION={$_.exportedcommands.count}}<\/p>\n<p>This works great. But one problem is that the output is not in order. To do this, I simply add <strong>Sort-Object<\/strong> at the end of the command. (One nice thing about creating a custom object as output is that I can use Windows PowerShell to further manipulate the output.) The following command is a single-line command that I have broken for readability on the blog.<\/p>\n<p style=\"padding-left: 30px\">Get-Module |<\/p>\n<p style=\"padding-left: 30px\">select name, @{LABEL=&#8217;cmdletCount&#8217;;EXPRESSION={$_.exportedcommands.count}} |<\/p>\n<p style=\"padding-left: 30px\">sort cmdletcount &ndash;Descending<\/p>\n<p>The command and the output associated with the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1817.HSG-3-10-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1817.HSG-3-10-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>I can even pipe the results to the <strong>Out-GridView<\/strong> cmdlet and have a graphical tool to filter my results. This is shown in the following code.<\/p>\n<p>Get-Module |<\/p>\n<p style=\"padding-left: 30px\">select name, @{LABEL=&#8217;cmdletCount&#8217;;EXPRESSION={[int]$_.exportedcommands.count}} |<\/p>\n<p style=\"padding-left: 30px\">sort cmdletcount -Descending |<\/p>\n<p style=\"padding-left: 30px\">Out-GridView<\/p>\n<p>The <strong>Out-GridView<\/strong> tool is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0020.HSG-3-10-13-03.png\"><img decoding=\"async\" title=\"Image of menu\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0020.HSG-3-10-13-03.png\" alt=\"Image of menu\" \/><\/a><\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about discovering cool information by playing around with Windows PowerShell modules. Microsoft Scripting Guy, Ed Wilson, is here. It is the weekend here in Charlotte, North Carolina in the United States. I am sipping a cup of oolong green tea with a bit of jasmine and cinnamon stick, [&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":[52,3,4,61,45],"class_list":["post-4042","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-modules","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about discovering cool information by playing around with Windows PowerShell modules. Microsoft Scripting Guy, Ed Wilson, is here. It is the weekend here in Charlotte, North Carolina in the United States. I am sipping a cup of oolong green tea with a bit of jasmine and cinnamon stick, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4042","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=4042"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4042\/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=4042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}