{"id":285,"date":"2018-04-24T00:01:16","date_gmt":"2018-04-24T08:01:16","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/koryt\/?p=285"},"modified":"2020-04-29T10:26:07","modified_gmt":"2020-04-29T17:26:07","slug":"doing-more-with-functions-comment-based-help","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/doing-more-with-functions-comment-based-help\/","title":{"rendered":"Doing More With Functions: Comment-Based Help"},"content":{"rendered":"<p>I just wanted to throw together a post highlighting how cool and easy it is to add help data to your own Functions and scripts.<\/p>\n<p>The help data gets added via comments. For functions the help data can go in three places:<\/p>\n<ol>\n<li>Before the function keyword (I like it up here)<\/li>\n<li>Between the open curly brace and the param() statement<\/li>\n<li>At the bottom of the function before the closing curly brace (I hate this spot)<\/li>\n<\/ol>\n<p>For scripts we just put it at the top of your script before you type the param() statement, or at the bottom, but the bottom will mess with code signing.<\/p>\n<p>Syntax just involves using a dot before the help keyword and then typing the help you want for it on the next line:<\/p>\n<pre class=\"lang:default decode:true\">&lt;#\r\n.&lt;keyword&gt;\r\n&lt;help data for it&gt;\r\n#&gt;<\/pre>\n<p>Here is a <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.core\/about\/about_comment_based_help?view=powershell-5.1\" target=\"_blank\" rel=\"noopener noreferrer\">list of all the keywords<\/a> I found and short descriptions of them:<\/p>\n<pre class=\"lang:ps decode:true\">&lt;#\r\n.SYNOPSIS\r\nA brief description of the function or script.\r\nThis keyword can be used only once in each topic.\r\n\r\n.DESCRIPTION\r\nA detailed description of the function or script.\r\nThis keyword can be used only once in each topic.\r\n\r\n.PARAMETER &lt;Parameter-Name&gt;\r\nThe description of a parameter.\r\nAdd a \".PARAMETER\" keyword for each parameter in the function or script syntax.\r\n\r\n.EXAMPLE\r\nA sample command that uses the function or script, optionally followed by sample output and a description.\r\nRepeat this keyword for each example.\r\n\r\n.INPUTS\r\nThe Microsoft .NET Framework types of objects that can be piped to the function or script.\r\nYou can also include a description of the input objects.\r\n\r\n.OUTPUTS\r\nThe .NET Framework type of the objects that the cmdlet returns.\r\nYou can also include a description of the returned objects.\r\n\r\n.NOTES\r\nAdditional information about the function or script.\r\n\r\n.LINK\r\nThe name of a related topic.\r\nThe value appears on the line below the \".LINK\" keyword and must be preceded by a comment symbol # or included in the comment block.\r\nRepeat the \".LINK\" keyword for each related topic.\r\n\r\nThe \"Link\" keyword content can also include a Uniform Resource Identifier (URI) to an online version of the same help topic. The online version opens when you use the Online parameter of Get-Help. The URI must begin with \"http\" or \"https\".\r\n\r\n.COMPONENT\r\nThe technology or feature that the function or script uses, or to which it is related.\r\n\r\n.ROLE\r\nThe user role for the help topic.\r\n\r\n.FUNCTIONALITY\r\nThe intended use of the function.\r\n\r\n.FORWARDHELPTARGETNAME &lt;Command-Name&gt;\r\nRedirects to the help topic for the specified command.\r\nYou can redirect users to any help topic, including help topics for a function, script, cmdlet, or provider.\r\n\r\n.FORWARDHELPCATEGORY &lt;Category&gt;\r\nSpecifies the help category of the item in \"ForwardHelpTargetName\".\r\nValid values are \"Alias\", \"Cmdlet\", \"HelpFile\", \"Function\", \"Provider\", \"General\", \"FAQ\", \"Glossary\", \"ScriptCommand\", \"ExternalScript\", \"Filter\", or \"All\".\r\nUse this keyword to avoid conflicts when there are commands with the same name.\r\n\r\n.REMOTEHELPRUNSPACE &lt;PSSession-variable&gt;\r\nSpecifies a session that contains the help topic.\r\nEnter a variable that contains a \"PSSession\".\r\n\r\n.EXTERNALHELP\r\nSpecifies an XML-based help file for the script or function.\r\n\r\n#&gt;\r\n\r\n<\/pre>\n<p>For the most part, I recommend using a few of the most common and important ones, which I think are:<\/p>\n<ul>\n<li>Description<\/li>\n<li>Parameter for every unclear parameter<\/li>\n<li>Examples to help your users<\/li>\n<\/ul>\n<p>Let&#8217;s try it<\/p>\n<pre class=\"lang:ps decode:true \">&lt;#\r\n.Description\r\nThis function takes in a message and writes it out to the screen in cyan.\r\n\r\n.Parameter Msg\r\nThis is the message that will be written to the screen in cyan\r\n\r\n.Example\r\nHelpTest -msg \"Hello world\"\r\n\r\n.Example\r\nHelpTest \"Hello World\"\r\n\r\n#&gt;\r\n\r\nfunction HelpTest\r\n{\r\nParam($msg)\r\nWrite-Host $msg -ForegroundColor Cyan\r\n}\r\n\r\nget-help helptest -showwindow<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86483\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/04\/Blog-help-windows-1007x1024.png\" alt=\"\" width=\"1007\" height=\"1024\" srcset=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/04\/Blog-help-windows-1007x1024.png 1007w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/04\/Blog-help-windows-1007x1024-295x300.png 295w, https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/04\/Blog-help-windows-1007x1024-768x781.png 768w\" sizes=\"(max-width: 1007px) 100vw, 1007px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Notice PowerShell numbers the examples for us in case we add more or move them around.<\/p>\n<p>To make tacking on the help data easier, I recommend you utilize snippets (ctrl+J OR edit-&gt;snippets) and make a custom one.<\/p>\n<pre class=\"lang:ps decode:true \">$helpText = @\"\r\n&lt;#\r\n.Description\r\n\r\n.Parameter\r\n\r\n.Example\r\n\r\n#&gt;\r\n\"@\r\n\r\nNew-IseSnippet -Title \"Help (Simple)\" -Text $helpText -Author KoryT -Description \"simple comment based help\"\r\n<\/pre>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-86484\" src=\"http:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2018\/04\/Blog-Help-snippet-300x203.png\" alt=\"\" width=\"300\" height=\"203\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Well, that&#8217;s all for now, hopefully this helps you polish off your tools and write help data the right way!<\/p>\n<p>This was originally written as part of\u00a0 my <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-for-programmers-a-quick-start-guide\/\">&#8220;PowerShell For Programmers&#8221;<\/a> series, though I might link it from other stuff in the future as well.<\/p>\n<p>Let me know in the comments if there is any particular topics you&#8217;re looking for more help with!<\/p>\n<p>If you find this helpful don&#8217;t forget to rate, comment and share\u00a0\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just wanted to throw together a post highlighting how cool and easy it is to add help data to your own Functions and scripts. The help data gets added via comments. For functions the help data can go in three places: Before the function keyword (I like it up here) Between the open curly [&hellip;]<\/p>\n","protected":false},"author":7300,"featured_media":87096,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1738,2126],"tags":[2221,2125],"class_list":["post-285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-powershell_for_programmers","tag-kory-thacher","tag-koryt"],"acf":[],"blog_post_summary":"<p>I just wanted to throw together a post highlighting how cool and easy it is to add help data to your own Functions and scripts. The help data gets added via comments. For functions the help data can go in three places: Before the function keyword (I like it up here) Between the open curly [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/285","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\/7300"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/285\/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=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}