{"id":202,"date":"2014-12-14T00:01:00","date_gmt":"2014-12-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/12\/14\/weekend-scripter-create-powershell-graphical-help-function\/"},"modified":"2019-02-18T10:36:35","modified_gmt":"2019-02-18T17:36:35","slug":"weekend-scripter-create-powershell-graphical-help-function","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-create-powershell-graphical-help-function\/","title":{"rendered":"Weekend Scripter: Create PowerShell Graphical Help Function"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about creating a graphical Help function in Windows PowerShell.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Well, I should have known. In yesterday&rsquo;s post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-a-graphical-tool-to-explore-powershell-help\/\" target=\"_blank\">A Graphical Tool to Explore PowerShell Help<\/a>, I said it would not take too long to convert my Windows PowerShell script (that I used to display Help) into a function. All of a sudden, I get a dozen emails to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> asking how to do that&mdash;not to mention a few tweets.<\/p>\n<p>As it turned out, I spent more than a few seconds converting the script into a function. Of course, I decided if I am going to go to the trouble of converting it to a function, I should add functionality. So I did.<\/p>\n<p>I added another grid so I could pick the module from the list of available modules. I also added switched parameters to allow me to change the level of Help displayed by the final <b>Get-Help<\/b> cmdlet. Along the way, I decided to use splatting to pass my parameters to the <b>Get-Help<\/b> cmdlet. It is way cool, and a worthwhile technique to use in other functions.<\/p>\n<p><b>&nbsp; &nbsp;Note <\/b>&nbsp;I have posted several blog articles about splatting over the years. To come up to speed on this very useful technique, <br \/>&nbsp; &nbsp;read these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/splatting\/\" target=\"_blank\">Hey, Scripting Guy! Blog posts<\/a>.<\/p>\n<p>I start my function by using the <b>Function<\/b><i> <\/i>keyword. I provide a name, open a script block, and define three switched parameters. Switched parameters only affect the script if they are present when calling the function. This portion of the script is shown here:<\/p>\n<p style=\"margin-left:30px\">Function Get-GraphicalHelp {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Param (<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$detailed,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$full,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$examples)<\/p>\n<p>The three switched parameters are mutually exclusive after I pass them to the <b>Get-Help<\/b> function. I used a very rudimentary technique to handle this issue of too many parameters. I check the number of parameters contained in the $<b>psBoundParameters <\/b>automatic variable to see if there is more than one item there. If there is, I use the <b>Throw<\/b><i> <\/i>keyword to raise an error and stop the script execution. It is not very graceful, but it works. A better way of doing this would be to create different parameter sets.<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> I talk about creating parameter sets in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-parameter-sets-to-simplify-powershell-commands\/\" target=\"_blank\">Use Parameter Sets to Simplify PowerShell Commands<\/a>.<\/p>\n<p>Here is the simple command that I use to prevent sending too many parameters to function:<\/p>\n<p style=\"margin-left:30px\">if ($psBoundParameters.count -gt 1) {throw &quot;only one parameter permitted&quot;}<\/p>\n<p>When I pass too many parameters to the <b>Get-GraphicalHelp<\/b> function, it generates the following error message (the <b>Throw<\/b><i> <\/i>command raises it):<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-14-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-12-14-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I use the <b>Get-Module &ndash;ListAvailable<\/b> command to retrieve all available modules. I select the name of each module, and send the results to the <b>Out-GridView<\/b> cmdlet. When I call <b>Out-GridView<\/b>, I use the <b>&ndash;PassThru<\/b> parameter so I can retrieve the item selected and store it in the <b>$m<\/b> variable. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">$m = Get-Module -ListAvailable | select-object name | Out-GridView -PassThru<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> I can select multiple modules from the grid. If this becomes a normal usage scenario, I might want to <br \/>&nbsp; &nbsp;add <b>ModuleName<\/b> to my cmdlet grid.<\/p>\n<p>The next portion of the function is the same script that I created yesterday. The <b>Select-Object<\/b> statement in this portion of the function is where I would add the <b>ModuleName<\/b><i> <\/i>property if I decided to choose multiple modules from the previous grid. This script is shown here:<\/p>\n<p style=\"margin-left:30px\">Get-Command -Module $m.name |<\/p>\n<p style=\"margin-left:30px\">Select-Object name, verb, noun, definition |<\/p>\n<p style=\"margin-left:30px\">Out-GridView -Title &quot;Cmdlets from the $m module&quot; -PassThru |<\/p>\n<p>To simplify the <b>Get-Help<\/b> command, I use splatting. The variable will always contain the parameter I supplied to the function, whether it is <b>&ndash;Full<\/b>, <b>&ndash;Detailed<\/b>, or <b>&ndash;Examples<\/b>. It does not matter because the parameters will be contained in the <b>$psBoundParameters<\/b> automatic variable. Therefore, all I need to do is to use splatting. Here is the command:<\/p>\n<p style=\"margin-left:30px\">Get-Help @psBoundParameters }<\/p>\n<p>That is all there is to it. The complete function is shown here:<\/p>\n<p style=\"margin-left:30px\">Function Get-GraphicalHelp {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Param (<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$detailed,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$full,<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [switch]$examples)<\/p>\n<p style=\"margin-left:30px\">if ($psBoundParameters.count -gt 1) {throw &quot;only one parameter permitted&quot;}<\/p>\n<p style=\"margin-left:30px\">$m = Get-Module -ListAvailable | select-object name | Out-GridView -PassThru<\/p>\n<p style=\"margin-left:30px\">Get-Command -Module $m.name |<\/p>\n<p style=\"margin-left:30px\">Select-Object name, verb, noun, definition |<\/p>\n<p style=\"margin-left:30px\">Out-GridView -Title &quot;Cmdlets from the $m module&quot; -PassThru |<\/p>\n<p style=\"margin-left:30px\">Get-Help @psBoundParameters }<\/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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating a graphical Help function in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Well, I should have known. In yesterday&rsquo;s post, A Graphical Tool to Explore PowerShell Help, I said it would not take too long to convert my Windows PowerShell script (that I used [&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":[69,463,3,562,170,61,45],"class_list":["post-202","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-help","tag-scripting-guy","tag-scripting-technique","tag-splatting","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about creating a graphical Help function in Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, is here. Well, I should have known. In yesterday&rsquo;s post, A Graphical Tool to Explore PowerShell Help, I said it would not take too long to convert my Windows PowerShell script (that I used [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/202","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=202"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/202\/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=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}