{"id":51653,"date":"2010-01-04T00:01:00","date_gmt":"2010-01-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/01\/04\/hey-scripting-guy-how-can-i-add-help-to-windows-powershell-scripts\/"},"modified":"2010-01-04T00:01:00","modified_gmt":"2010-01-04T00:01:00","slug":"hey-scripting-guy-how-can-i-add-help-to-windows-powershell-scripts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-add-help-to-windows-powershell-scripts\/","title":{"rendered":"Hey, Scripting Guy! How Can I Add Help to Windows PowerShell Scripts?"},"content":{"rendered":"<p class=\"MsoNormal\">&nbsp;<a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I was told by the script committee at work that I must add Help to my scripts if they are to be stored in the official script repository we have at work. Can you tell me how I would go about doing this?<\/p>\n<p class=\"MsoNormal\">&#8212; CG<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello CG, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I am listening to Johnny Cash on my Zune HD and drinking <a href=\"http:\/\/en.wikipedia.org\/wiki\/Espresso\"><font face=\"Segoe\">espresso<\/font><\/a> this morning. Normally, I would not drink espresso in the morning because I prefer it in the afternoon, but this morning seems like afternoon or evening or something. It has been a rather long night as I press on to my goal of getting all the Hey, Scripting Guy! posts written so that Craig, the Scripting Editor, can take a much deserved holiday. He actually sent me an e-mail challenge yesterday. So I dug out the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Espresso_machine#Steam-driven\"><font face=\"Segoe\">simple steam-driven espresso machine<\/font><\/a> I brought back from Naples, Italy, many years ago, placed it on the stove, and made a pot of midnight black espresso. I like to have a small piece of <span lang=\"PT\"><a href=\"http:\/\/pt.wikipedia.org\/wiki\/L%C3%ADngua_de_gato_(chocolate)\">L&iacute;ngua de gato<\/a><em>,<\/em> which is a habit I acquired while I was teaching a VBScript class in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Sao_paulo\"><font face=\"Segoe\">Sao Paulo<\/font><\/a> a couple years ago.&nbsp;<\/span><\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows PowerShell 2.0 Best Practices book\" alt=\"Image of Windows PowerShell 2.0 Best Practices book\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/november\/hey1123\/hsg-11-23-09-03.jpg\" width=\"197\" height=\"240\"><\/p>\n<p class=\"Readeraidonly\">Note: Portions of today&#8217;s Hey, Scripting Guy! article are excerpted from the Microsoft Press book, <em>Windows PowerShell 2.0 Best Practices<\/em> by Ed Wilson. This book is <a href=\"http:\/\/bit.ly\/1NBxZ0\"><font face=\"Segoe\">available for pre-order<\/font><\/a>.<\/p>\n<p class=\"MsoNormal\">In Windows PowerShell 1.0, if you wanted to include a comment that spans multiple lines, you used multiple pound characters (<b>#<\/b>), as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\"># This is the first line of a multiple line comment<br \/># This is the second line of the comment<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">This works fine for short comments, but when you need to write a paragraph documenting a particular feature, or construction in the script, it becomes rather annoying with the need to type all of the pound characters. If one of the comment characters is inadvertently left out, an error will be generated. Depending on the actual placement of the pound sign, the error could be misleading and cause you to waste development time by chasing down the errant line of code. You can still use the multiple pound character approach to adding comments in Windows PowerShell 2.0 if you wish to do so. The advantage is simplicity and backward compatibility with Windows PowerShell 1.0. Your code will also be easy to read: Anyone familiar with Windows PowerShell will be able to immediately recognize the lines of code as comments. <\/p>\n<p class=\"MsoNormal\">One trick that could be used in Windows PowerShell 1.0 to allow for multi-line comments was to use a here-string. The here-string allows you to assign text without worrying about line formatting, or escaping quotation marks and other special characters. It is helpful for working with multiple lines of text because it allows you to forget about the more tedious aspects of working with formatted text, such as escaping quotation marks.<span>&nbsp; <\/span><\/p>\n<p class=\"MsoNormal\">An example of working with here-strings is the Demo-HereString.ps1 script. The variable <b>$instructions<\/b> is used to hold the content of the here-string. The actual here-string itself is created by beginning the string with the ampersand double quotation mark (<b>@&#8221;<\/b>) symbol. The here-string is terminated by reversing the order: double quotation mark ampersand (&#8220;@) symbol. Everything between the two tags is interpreted as a string, including special characters. The here-string from the Demo-HereString.ps1 script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$instructions = @&#8221;<br \/>This command-line demo illustrates working with multiple lines <br \/>of text. The cool thing about using a here-string is that it allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>&#8220;@<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The here-string is displayed by calling the variable that contains the here-string. In the Demo-HereString.ps1 script, the response to a prompt posed by the <b>Read-Host<\/b> cmdlet is stored in the <b>$response<\/b> variable. The <b>Read-Host<\/b> command is seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$response = Read-Host -Prompt &#8220;Do you need instructions? &lt;y \/ n&gt;&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The value stored in the <b>$response<\/b> variable is then evaluated by the <b>if<\/b> statement. If the value is equal to the letter <b>y<\/b>, the contents of the here-string are displayed. If the value of the <b>$response<\/b> variable is equal to anything else, the script displays the string &ldquo;good bye&rdquo; and exits. This section of the script is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">if ($response -eq &#8220;y&#8221;) { $instructions ; exit }<br \/>else { &#8220;good bye&#8221; ; exit }The Demo-HereString.ps1 script is seen here. <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The complete Demo-HereString.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>Demo-HereString.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$instructions = @&#8221;<br \/>This command-line demo illustrates working with multiple lines <br \/>of text. The cool thing about using a here-string is that it allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>&#8220;@<\/p>\n<p>$response = Read-Host -Prompt &#8220;Do you need instructions? &lt;y \/ n&gt;&#8221;<br \/>if ($response -eq &#8220;y&#8221;) { $instructions ; exit }<br \/>else { &#8220;good bye&#8221; ; exit } <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The advantage of using a here-string to store your comments is that it then becomes easy to create named sections of comments. If you have named comment sections, you can then solicit input from a user and then display the appropriate section of Help text for the user of the script. In the Demo-HereString1.ps1 script, three different levels of Help are created: Overview, Instructions, and Details. The results of the three different here-strings are stored in three different variables. The overview Help string provides a quick three-line summary, as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$overview=@&#8221;<br \/>OVERVIEW<br \/>The here-string provides an easy way to add<br \/>multi-line comments to a script. It can also<br \/>hold additional information if desired.<br \/>&#8220;@<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>$instructions <\/b>variable is used to hold additional information about here-strings, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$instructions = @&#8221;<br \/>INSTRUCTIONS<br \/>This command-line demo illustrates working with multiple lines <br \/>of text. The cool thing about using a here-string is that it allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>&#8220;@<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>$details<\/b> variable holds a literal here-string. You can create a literal here-string the same way that you create a regular here-string, except that you use a single quotation mark following the at symbol (@). This is seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$details=@&#8217;<br \/>DETAILS<br \/>When working with the here-string, it is important that the @&#8221; character<br \/>be immediately followed with a &lt;cr&gt;; you can then type test as required.<br \/>The cool thing about using a here-string is that it allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>When you are done you close you&rsquo;re here-string by placing the &#8220;@ on its own<br \/>individual line.<br \/>If you wish to use the @&#8221; character or the &#8220;@ character in a here-string, use the<br \/>non-expanding here-string, which uses the @&#8217; character or the &#8216;@ character.<br \/>One problem with using the exit statement in the ISE is that it closes the <br \/>Windows PowerShell ISE, which is annoying. <br \/>&#8216;@<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The <b>switch<\/b> statement is used to evaluate the value that is supplied to the <b>Read-Host<\/b> prompt. Note that if the value is not one of the three valid replies&mdash;o, i, or d&mdash;the default action will display a message and exit the script. When using the <b>exit<\/b> command in the Windows PowerShell ISE, it will close the Windows PowerShell ISE, which can be a bit annoying. I, therefore, always use the Windows PowerShell console when testing a script that includes the <b>exit<\/b> condition. <\/p>\n<p class=\"Readeraidonly\">For more information on using the <b>Switch<\/b> statement in Windows PowerShell, see <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/05\/07\/how-can-i-evaluate-a-condition-and-select-from-several-options-with-windows-powershell.aspx\"><font face=\"Segoe\">How Can I Evaluate a Condition and Select from Several Options with Windows PowerShell?<\/font><\/a> <\/p>\n<p class=\"MsoNormal\">The complete Demo-HereString1.ps1 script is seen here.<\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>Demo-HereString1.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$overview=@&#8221;<br \/>OVERVIEW<br \/>The here-string provides an easy way to add<br \/>multi-line comments to a script. It can also<br \/>hold additional information if desired.<br \/>&#8220;@<br \/>$instructions = @&#8221;<br \/>INSTRUCTIONS<br \/>This command-line demo illustrates working with multiple lines <br \/>of text. The cool thing about using a here-string is that is allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>&#8220;@<\/p>\n<p>$details=@&#8217;<br \/>DETAILS<br \/>When working with the here-string, it is important that the @&#8221; character<br \/>be immediately followed with a &lt;cr&gt;; you can then type test as required.<br \/>The cool thing about using a here-string is that it allows<br \/>you to &#8220;work&#8221; with text without the need to &#8220;worry&#8221; about quoting<br \/>or other formatting issues.<br \/><span>&nbsp;&nbsp; <\/span>It even allows you <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>a sort of <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>WYSIWYG type of experience. <br \/>You format the data as you wish it to appear. <br \/>When you are done, you close you&rsquo;re here-string by plac\ning the &#8220;@ on its own<br \/>individual line.<br \/>If you wish to use the @&#8221; character or the &#8220;@ character in a here-string, use the<br \/>non-expanding here-string, which uses the @&#8217; character or the &#8216;@ character.<br \/>One problem with using the exit statement in the ISE is that it closes the <br \/>Windows PowerShell ISE, which is annoying. <br \/>&#8216;@<\/p>\n<p>$response = Read-Host -Prompt &#8220;Do you need Overview, Instructions, or Details &lt;o\/i\/d&gt;&#8221;<br \/>Switch ($response)<br \/>{<br \/>&#8220;o&#8221; {$Overview ; exit }<br \/>&#8220;i&#8221; { $instructions ; exit }<br \/>&#8220;d&#8221; {$details ; exit }<br \/><span>&nbsp;<\/span>default { &#8220;good bye&#8221; ; exit } <br \/>} #end switch<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">When you run the Demo-HereString1.ps1 script and run through the different command-line options, you will be presented the output seen here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of output of script\" alt=\"Image of output of script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/january\/hey0104\/hsg-01-04-10-01.jpg\" width=\"600\" height=\"500\"><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">CG, that is all there is to using here-strings for Help comments. <span>&nbsp;Windows <\/span>PowerShell Help Week will continue tomorrow when we will talk about&hellip;not so fast. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post them on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I was told by the script committee at work that I must add Help to my scripts if they are to be stored in the official script repository we have at work. Can you tell me how I would go about doing this? &#8212; CG &nbsp; Hello CG, Microsoft Scripting [&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":[51,3,45],"class_list":["post-51653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Hey, Scripting Guy! I was told by the script committee at work that I must add Help to my scripts if they are to be stored in the official script repository we have at work. Can you tell me how I would go about doing this? &#8212; CG &nbsp; Hello CG, Microsoft Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51653","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=51653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51653\/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=51653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}