{"id":17141,"date":"2010-09-12T00:01:00","date_gmt":"2010-09-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/12\/modify-the-powershell-ise-to-automatically-add-a-script-header\/"},"modified":"2010-09-12T00:01:00","modified_gmt":"2010-09-12T00:01:00","slug":"modify-the-powershell-ise-to-automatically-add-a-script-header","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/modify-the-powershell-ise-to-automatically-add-a-script-header\/","title":{"rendered":"Modify the PowerShell ISE to Automatically Add a Script Header"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: Customize the Windows PowerShell ISE by automatically adding custom headers to new scripts. The Microsoft Scripting Guys show you how.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Oliver, my friend in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Regensburg\">Regensburg<\/a>, Germany, was having a problem adding a user to a group from another forest. He kept asking me questions when I was either in a meeting, working on a Scripting Guy article, or talking on the phone&mdash;it was getting embarrassing because I really wanted to help him. Finally, I decided to use Outlook 2010 to schedule a <a href=\"http:\/\/www.microsoft.com\/business\/en-us\/products\/officelivemeeting\/default.aspx\">Live Meeting<\/a> with him. <\/p>\n<p>The time arrived, and we spent a cool hour working through some of the issues. Today, he sent me an IM that said he had the script working. He has promised to write a Guest Blogger article about the script and the issues he was having. Interestingly enough, I did not meet Oliver when I was in Regensburg. It was only after I became a Scripting Guy that I met him. Regensburg is a great place to take pictures. The following photo is one I took during my time there while I was teaching a <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> class. <\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Photo Ed took in Regensburg, Germany\" border=\"0\" alt=\"Photo Ed took in Regensburg, Germany\" src=\"https:\/\/docs.microsoft.com\/\" width=\"554\" height=\"416\" \/><\/a> <\/p>\n<p>It is not that I did not want to help Oliver, but that my time just seems to get away from me. I have a very heavy schedule, which means I rely extensively on Microsoft Outlook 2010 reminders, tasks, and appointments. If I were ever to lose my Outlook data, I would be in serious trouble. Hmmm, maybe I need to write a script to back up Outlook data. Just added that to my Outlook tasks. <\/p>\n<p>It was not on my calendar, but I decided to add a function to my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/profiles\/\">Windows PowerShell profile<\/a> that would add the header information to a script in the Windows PowerShell ISE. This is something I have been doing manually for nearly two years. It does not take but a minute for me to type this information, but if you think about all the Windows PowerShell scripts I have written in the last two years, the function would have saved me at least enough time to take a day off.<\/p>\n<p>The Add-HeaderToScript function is shown here. <\/p>\n<blockquote>\n<p><strong>Add-HeaderToScript<\/strong><\/p>\n<p><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-HeaderToScript<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">.Synopsis<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">This<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">adds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">header<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">information<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">&nbsp; <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">Add-HeaderToScript<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">Adds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">header<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">comments<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">&nbsp; <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\">&nbsp; <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">AH<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">Uses<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">alias<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">add<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">header<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">comments<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">.Notes<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">NAME:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add-HeaderToScript<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">AUTHOR:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Ed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Wilson,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">msft<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">LASTEDIT:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">09<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">07<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">2010<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">19<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">28<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">KEYWORDS:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Scripting<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Techniques,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">HSG:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">WES-09-12-10<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp; <\/span><span style=\"color: #000000\">.Link<\/span><span style=\"color: #808080\">       <br \/>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">Http:\/\/www.ScriptingGuys.com<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">Requires<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Version<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$header<\/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 \/># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;        <br \/># Script: $(split-path -Path $psISE.CurrentFile.FullPath -Leaf)        <br \/># Author: $env:username        <br \/># Date: $(Get-Date)        <br \/># Keywords:        <br \/># comments:        <br \/>#        <br \/># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;        <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\">$header<\/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-headertoscript<\/span><span style=\"color: #808080\">&nbsp;<\/span> <\/p>\n<\/blockquote>\n<p>The comment-based help was added by the <strong>Add-Help<\/strong> function I added to my Windows PowerShell ISE profile <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts.aspx\">yesterday<\/a>. <\/p>\n<p>The same technique for the <strong>Add-HeaderToScript<\/strong> function is followed that was used in the <strong>Add-Help<\/strong> function. A <strong>here-string<\/strong> holds the header information, and the <strong>InsertText<\/strong> method adds the header at the current insertion point using the command shown here:<\/p>\n<blockquote>\n<p><span style=\"color: #2b91af\">$psise<\/span><span style=\"color: #000000\">.CurrentFile.Editor.InsertText(<\/span><span style=\"color: #2b91af\">$header<\/span><span style=\"color: #000000\">)<\/span> <\/p>\n<\/blockquote>\n<p>Because an expanding <strong>here-string<\/strong> is used, it means that many of the values required by the header block can be obtained automatically. For example, if the script has been saved, the command shown here will return the name of the current script:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000ff\">split-path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$psISE<\/span><span style=\"color: #000000\">.CurrentFile.FullPath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Leaf)<\/span> <\/div>\n<\/blockquote>\n<p>If the script has not been saved, the <strong>Script:<\/strong> field will be set to untitled.ps1. The <strong>Author:<\/strong> field and the <strong>Date:<\/strong> field of the header block are filled using the same techniques that were used in yesterday&rsquo;s <strong>Add-Help<\/strong> function. Again, if you do not like the user name stored in the <strong>$env:username<\/strong> environmental variable, you can hard code the value for the <strong>Author:<\/strong> field. <\/p>\n<p>Because <strong>Add-HeaderToScript<\/strong> is kind of long to type, you have two choices. You can use tab expansion and type <strong>Add-He<\/strong> and press Tab, which will complete the function name. Or you can create an alias. I added the following command to my Windows PowerShell ISE profile to create an alias called <strong>ah<\/strong> to call the function. Because it is possible you might have an alias <strong>ah<\/strong> already used (it is not taken by default), I added a test for the <strong>ah<\/strong> alias, and only create it if the alias is available. The code is shown here:<\/p>\n<p><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(!(<\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">alias:ah))<\/span><span style=\"color: #808080\">     <\/p>\n<p>&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">     <\/p>\n<p>&nbsp; <\/span><span style=\"color: #0000ff\">New-Alias<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ah<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">add-headertoscript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Description<\/span><span style=\"color: #808080\"> &#8220;MrEd alias&#8221; <\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">     <\/p>\n<p>&nbsp; <\/span><span style=\"color: #0000ff\">Out-Null<\/span><span style=\"color: #808080\">     <\/p>\n<p>&nbsp; <\/span><span style=\"color: #000000\">}<\/span> <\/p>\n<p>&nbsp;<\/p>\n<p>When I run the <strong>Add-HeaderToScript<\/strong> function by using the <strong>ah<\/strong> alias, the header shown in the following image is added to the script. Note that this script was previously saved with the name that appears in the <strong>Script:<\/strong> field. <\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" title=\"Image of header being added to script\" border=\"0\" alt=\"Image of header being added to script\" src=\"https:\/\/docs.microsoft.com\/\" width=\"554\" height=\"391\" \/><\/a> <\/p>\n<p>Well, I think I am going to break out my saxophone, go outside, and annoy the neighbors for a while. Actually, the neighbor&rsquo;s dog is a real fan and cannot resist the temptation to sing along with me. He does not seem to know all the words to &ldquo;Sweet Home Chicago&rdquo; but he really gets into the spirit. <\/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:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow when we will begin Guest Blogger Week. We have some absolutely fantastic posts lined up. 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>&nbsp; Summary: Customize the Windows PowerShell ISE by automatically adding custom headers to new scripts. The Microsoft Scripting Guys show you how. &nbsp; Microsoft Scripting Guy Ed Wilson here. Oliver, my friend in Regensburg, Germany, was having a problem adding a user to a group from another forest. He kept asking me questions when I [&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-17141","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>&nbsp; Summary: Customize the Windows PowerShell ISE by automatically adding custom headers to new scripts. The Microsoft Scripting Guys show you how. &nbsp; Microsoft Scripting Guy Ed Wilson here. Oliver, my friend in Regensburg, Germany, was having a problem adding a user to a group from another forest. He kept asking me questions when I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17141","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=17141"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17141\/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=17141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}