{"id":144,"date":"2011-03-15T14:33:06","date_gmt":"2011-03-15T20:33:06","guid":{"rendered":"http:\/\/ryan.witschger.net\/?p=144"},"modified":"2011-03-15T14:33:06","modified_gmt":"2011-03-15T20:33:06","slug":"adding-help-to-a-powershell-script","status":"publish","type":"post","link":"http:\/\/www.Get-Blog.com\/?p=144","title":{"rendered":"Adding Help to a PowerShell Script"},"content":{"rendered":"<p>With the scripting games coming up, I noticed that they will be adding stars to your work based on the addition of script help blocks that are compatible with the PowerShell &#8220;Comment Based Help&#8221;.  Basically, this is a form of writing a comment block that works in conjunction with the get-help cmdlet that is included with PowerShell.<\/p>\n<p>With this post I am hoping spread the information that this is possible just as much as the how to do it.  I think that most script writers don&#8217;t even know it&#8217;s possible, and I&#8217;ll be honest that while I&#8217;ve know that you can do it, I don&#8217;t include in all of my own scripts.<\/p>\n<p>So, you can add comment based help either to a script or to a function.  It is my opinion that you do not need to add the help to a function within a script as no one will be able to run a get-help against it; however, using the format provided to label your functions is probably a good idea.<\/p>\n<p>So, there are two ways to make a comment block and three places to put them.  <\/p>\n<p>You can start all the lines in a block with # or you can surround all the lines with <# ...comments... #>.  Either form is fine, but the later might be easier for large block; however, the former might be easier to follow if browsing the code.  I typically like to start my lines with # as I can quickly see where all the comments start and stop.<\/p>\n<p>As far as where to place them, you can put them at the start of a function or script, at the end of a function or script,or just before the start of a function.  Line breaks become very important as spaces between comments and their attached objects will detach them.<\/p>\n<p>Now the comments need to follow the format:<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;\"><div class=\"powershell codecolorer\"><span class=\"co1\"># .KEYWORD<\/span><br \/>\n<span class=\"co1\"># &nbsp;Content<\/span><br \/>\n<span class=\"co1\">#<\/span><br \/>\n<span class=\"co1\"># .SYNOPSIS<\/span><br \/>\n<span class=\"co1\"># This is an example of comment based help.<\/span><\/div><\/div>\n<p>So what kind of keywords are there?  This is taken from the help file:<\/p>\n<div class=\"codecolorer-container powershell solarized-dark\" style=\"overflow:auto;white-space:nowrap;width:680px;height:300px;\"><div class=\"powershell codecolorer\">&nbsp; &nbsp; .SYNOPSIS<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; A brief description of the <span class=\"kw3\">function<\/span> or script. This keyword can be used<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; only once <span class=\"kw3\">in<\/span> each topic.<br \/>\n<br \/>\n&nbsp; &nbsp; .DESCRIPTION<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; A detailed description of the <span class=\"kw3\">function<\/span> or script. This keyword can be<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; used only once <span class=\"kw3\">in<\/span> each topic.<br \/>\n<br \/>\n&nbsp; &nbsp; .PARAMETER &nbsp;<span class=\"sy0\">&lt;<\/span>Parameter<span class=\"sy0\">-<\/span>Name<span class=\"sy0\">&gt;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The description of a parameter. You can include a Parameter keyword <span class=\"kw3\">for<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; each parameter <span class=\"kw3\">in<\/span> the <span class=\"kw3\">function<\/span> or script syntax.<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The Parameter keywords can appear <span class=\"kw3\">in<\/span> any order <span class=\"kw3\">in<\/span> the comment block<span class=\"sy0\">,<\/span>but<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; the <span class=\"kw3\">function<\/span> or script syntax determines the order <span class=\"kw3\">in<\/span> which the parameters<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#40;<\/span>and their descriptions<span class=\"br0\">&#41;<\/span> appear <span class=\"kw3\">in<\/span> Help topic. To change the order<span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; change the syntax.<br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; You can also specify a parameter description by placing a comment <span class=\"kw3\">in<\/span> the<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">function<\/span> or script syntax immediately before the parameter variable name.<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">If<\/span> you use both a syntax comment and a Parameter keyword<span class=\"sy0\">,<\/span> the description<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; associated with the Parameter keyword is used<span class=\"sy0\">,<\/span> and the syntax comment is<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ignored.<br \/>\n<br \/>\n&nbsp; &nbsp; .EXAMPLE<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; A sample command that uses the <span class=\"kw3\">function<\/span> or script<span class=\"sy0\">,<\/span> optionally followed<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; by sample output and a description. Repeat this keyword <span class=\"kw3\">for<\/span> each example.<br \/>\n<br \/>\n&nbsp; &nbsp; .INPUTS<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The Microsoft .NET Framework types of objects that can be piped to the<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">function<\/span> or script. You can also include a description of the input <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; objects.<br \/>\n<br \/>\n&nbsp; &nbsp; .OUTPUTS<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The .NET Framework <span class=\"kw2\">type<\/span> of the objects that the cmdlet returns. You can<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; also include a description of the returned objects.<br \/>\n<br \/>\n&nbsp; &nbsp; .NOTES<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Additional information about the <span class=\"kw3\">function<\/span> or script.<br \/>\n<br \/>\n&nbsp; &nbsp; .LINK<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The name of a related topic. Repeat this keyword <span class=\"kw3\">for<\/span> each related topic.<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; This content appears <span class=\"kw3\">in<\/span> the Related Links section of the Help topic.<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; The Link keyword content can also include a Uniform Resource Identifier<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#40;<\/span>URI<span class=\"br0\">&#41;<\/span> to an online version of the same Help topic. The online version <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; opens when you use the Online parameter of <span class=\"kw1\">Get-Help<\/span>. The URI must begin<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; with <span class=\"st0\">&quot;http&quot;<\/span> or <span class=\"st0\">&quot;https&quot;<\/span>.<\/div><\/div>\n<p>There are even more past that, but I think that covers the main group.  If you want to see them all and get more detailed information go to PowerShell and run <code class=\"codecolorer powershell default\"><span class=\"powershell\"><span class=\"kw1\">Get-Help<\/span> about_Comment_Based_Help<\/span><\/code>.<\/p>\n<p>As well, there are several built in sections that are generated automatically: Name, Syntax, Parameter List, Common Parameters, Parameter Attribute Table, Remarks.<\/p>\n<p>To get more information and some good examples go to PowerShell and run <code class=\"codecolorer powershell default\"><span class=\"powershell\"><span class=\"kw1\">Get-Help<\/span> about_Comment_Based_Help<\/span><\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the scripting games coming up, I noticed that they will be adding stars to your work based on the addition of script help blocks that are compatible with the PowerShell &#8220;Comment Based Help&#8221;.  Basically, this is a form of writing a comment block that works in conjunction with the get-help cmdlet that is included with PowerShell.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/144"}],"collection":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=144"}],"version-history":[{"count":0,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=\/wp\/v2\/posts\/144\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.Get-Blog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}