{"id":17151,"date":"2010-09-11T00:01:00","date_gmt":"2010-09-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts\/"},"modified":"2010-09-11T00:01:00","modified_gmt":"2010-09-11T00:01:00","slug":"automatically-add-comment-based-help-to-your-powershell-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/automatically-add-comment-based-help-to-your-powershell-scripts\/","title":{"rendered":"Automatically Add Comment-Based Help to Your PowerShell Scripts"},"content":{"rendered":"<p><strong>Summary<\/strong>: Automatically add comment-based help to your Windows PowerShell scripts; learn how in this how-to article by Scripting Guy Ed Wilson.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Last Sunday, I spent the morning creating a <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/05\/how-to-create-a-customizable-profile-for-the-powershell-ise.aspx\">Windows PowerShell ISE profile<\/a>. Today, I thought I would add something to that profile that will make my life easier. When I was creating the conversion module back in the series of articles I wrote about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/modules\/\">creating and using modules<\/a>, I created a text file that I called my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/02\/06\/hey-scripting-guy-february-6-2010.aspx\">help template<\/a>. When I wish to add comment-based help to a script or to a function, I have to do the following:<\/p>\n<ol>\n<li>Use search to locate the help text file.<\/li>\n<li>Open the help text file in Notepad.<\/li>\n<li>Copy the text of the file to the clipboard.<\/li>\n<li>Switch to the Windows PowerShell ISE.<\/li>\n<li>Paste the contents of the clipboard to the script or function that is under development.<\/li>\n<li>Switch back to Notepad.<\/li>\n<li>Close the help text file.<\/li>\n<li>Close the Windows Search results Explorer pane.<\/li>\n<li>Switch back to the Windows PowerShell ISE.<\/li>\n<li>Edit the comment-based text in the Windows PowerShell ISE.<\/li>\n<li>Add the current date to the last edit field of the comment.<\/li>\n<\/ol>\n<p>Granted, it does not take a long time to perform these 11 steps, but it is a lot of mousing and typing, and it does take several minutes to complete. As a result, I do not add comment-based help to my scripts or functions as often as I should. <\/p>\n<p>Now that I have a Windows PowerShell ISE profile, it makes sense to add a function to automatically add comment-based help to my Windows PowerShell scripts and functions. The function is called Add-Help and is shown here. <\/p>\n<blockquote>\n<p><strong>Add-Help<\/strong><\/p>\n<p><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-Help<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$helpText<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@<\/span><span style=\"color: #808080\">&#8221;       <br \/> &lt;#        <br \/>&nbsp;&nbsp; .Synopsis        <br \/>&nbsp;&nbsp;&nbsp; This does that&nbsp; <br \/>&nbsp;&nbsp; .Example        <br \/>&nbsp;&nbsp;&nbsp; Example-        <br \/>&nbsp;&nbsp;&nbsp; Example- accomplishes&nbsp; <br \/>&nbsp;&nbsp; .Parameter&nbsp; <br \/>&nbsp;&nbsp;&nbsp; The parameter        <br \/>&nbsp;&nbsp; .Notes        <br \/>&nbsp;&nbsp;&nbsp; NAME: Example-        <br \/>&nbsp;&nbsp;&nbsp; AUTHOR: $env:username        <br \/>&nbsp;&nbsp;&nbsp; LASTEDIT: $(Get-Date)        <br \/>&nbsp;&nbsp;&nbsp; KEYWORDS:        <br \/>&nbsp;&nbsp; .Link        <br \/>&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com        <br \/> #Requires -Version 2.0        <br \/> #&gt;        <br \/>&#8220;<\/span><span style=\"color: #000000\">@<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$psise<\/span><span style=\"color: #000000\">.CurrentFile.Editor.InsertText(<\/span><span style=\"color: #2b91af\">$helpText<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">add-help<\/span> <\/p>\n<\/blockquote>\n<p>The first thing I do in the <strong>Add-Help<\/strong> function is create a <strong>here-string<\/strong>. The <strong>here-string<\/strong> allows me to not worry about quoting rules or other coding conventions. Everything between the <strong>@&rdquo;<\/strong> and the <strong>&ldquo;@ <\/strong>symbols is interpreted as a string. Just as there are two kinds of regular strings&mdash;literal and expanding&mdash;so too are there two kinds of <strong>here-strings<\/strong>, literal and expanding. I am using the expanding <strong>here-string<\/strong> so that I can get the date to update. In addition, the username is pulled from the environmental <strong>username<\/strong> variable. If you do not want to use the environmental <strong>username<\/strong> variable for your script user name, you can hard code this value (this is in fact what I do in my personal Windows PowerShell ISE profile). <\/p>\n<p>Beyond the expanding <strong>here-string<\/strong>, the only really interesting thing about the function is the line of code that inserts text (obviously, this takes place at the current insertion point). To do this, I use the <strong>InsertText<\/strong> method from the <strong>Editor<\/strong> object from the <strong>CurrentFile<\/strong> object. This code is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$psise<\/span><span style=\"color: #000000\">.CurrentFile.Editor.InsertText(<\/span><span style=\"color: #2b91af\">$helpText<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>To add the <strong>Add-Help<\/strong> function to my Windows PowerShell ISE profile, I use the <strong>Set-Profile<\/strong> function I created last Sunday. It opens my profile in the Windows PowerShell ISE. Next I simply paste the function in the profile as shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6366.WES09111001_5D63E7C2.jpg\"><img decoding=\"async\" height=\"435\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3036.WES09111001_thumb_00643366.jpg\" alt=\"Image of pasting function into profile in Windows PowerShell ISE\" border=\"0\" title=\"Image of pasting function into profile in Windows PowerShell ISE\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>After I have saved the Windows PowerShell ISE profile, and closed and reopened the Windows PowerShell ISE. I now place my insertion point to the first line under the opening brace (curly bracket) of the <strong>Add-Help<\/strong> function, and I use the <strong>Add-Help<\/strong> function to add help to my <strong>Add-Help<\/strong> function. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0407.WES09111002_38A2AA7E.jpg\"><img decoding=\"async\" height=\"430\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1805.WES09111002_thumb_10FC2E54.jpg\" alt=\"Image of adding help to Add-Help function\" border=\"0\" title=\"Image of adding help to Add-Help function\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>One thing to keep in mind is that by default, my <strong>Add-Help<\/strong> function adds the <strong>.Parameter<\/strong> comment. If you do not have any parameters, or if you do not use this comment properly, your help will not work with the <strong>Get-Help<\/strong> cmdlet. If you do not intend to use this comment, delete it before adding the <strong>Add-Help<\/strong> function to your profile. <\/p>\n<p>&nbsp;<\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Automatically add comment-based help to your Windows PowerShell scripts; learn how in this how-to article by Scripting Guy Ed Wilson. &nbsp; Microsoft Scripting Guy Ed Wilson here. Last Sunday, I spent the morning creating a Windows PowerShell ISE profile. Today, I thought I would add something to that profile that will make my life [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,61,45,100],"class_list":["post-17151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>Summary: Automatically add comment-based help to your Windows PowerShell scripts; learn how in this how-to article by Scripting Guy Ed Wilson. &nbsp; Microsoft Scripting Guy Ed Wilson here. Last Sunday, I spent the morning creating a Windows PowerShell ISE profile. Today, I thought I would add something to that profile that will make my life [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17151","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17151"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17151\/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=17151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}