{"id":17804,"date":"2008-12-31T07:16:31","date_gmt":"2008-12-31T15:16:31","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/powershell\/?p=17804"},"modified":"2019-06-07T07:17:55","modified_gmt":"2019-06-07T15:17:55","slug":"powershell_ise-scripting-convertto-comment","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/powershell_ise-scripting-convertto-comment\/","title":{"rendered":"PowerShell_ISE Scripting: ConvertTo-Comment"},"content":{"rendered":"<p><P>Let me combine a couple of things.&nbsp; <\/P>\n<P>1) When I posted my PowerShell_ISE profile, I included a function Goto-Line that is now builtin to the ISE so I reposted the blog with this function commented out. I commented it out instead of deleting it because I wanted to provide an example of how to do it.<\/P>\n<P>2) Vivek Sharma left a comment asked about programmatically controlling indentation so I gave a couple of pointers to the objects required to program the editor.<\/P>\n<P>So let\u2019s take those 2 and put them together and write a function which will convert the selected text into a PowerShell comment (using another CTP3 feature \u2013 block comments\u201d).&nbsp; <\/P>\n<P>Here goes:<\/P>\n<P><FONT color=#0000ff size=2 face=Consolas>function ConvertTo-Comment <BR>{ <BR>&nbsp;&nbsp;&nbsp; $editor = $psISE.CurrentOpenedFile.Editor <BR>&nbsp;&nbsp;&nbsp; $CommentedText = &#8220;&lt;#`n&#8221; + $editor.SelectedText + &#8220;#&gt;&#8221; <BR>&nbsp;&nbsp;&nbsp; # INSERTING overwrites the SELECTED text <BR>&nbsp;&nbsp;&nbsp; $editor.InsertText($CommentedText) <BR>} <BR>$null = $psISE.CustomMenu.Submenus.Add(&#8220;Comment Selected&#8221;, {ConvertTo-Comment}, $null)<\/FONT><\/P>\n<P><FONT color=#0000ff size=2 face=Consolas><\/FONT>&nbsp;<\/P>\n<P>Give that a try and then start experimenting on your own.&nbsp; Think <A href=\"http:\/\/en.wikipedia.org\/wiki\/Text_Editor_and_Corrector\" target=_blank mce_href=\"http:\/\/en.wikipedia.org\/wiki\/Text_Editor_and_Corrector\">TECO<\/A>,&nbsp; <A href=\"http:\/\/h71000.www7.hp.com\/doc\/73final\/6018\/6018pro.html\" target=_blank mce_href=\"http:\/\/h71000.www7.hp.com\/doc\/73final\/6018\/6018pro.html\">TPU<\/A>, <A href=\"http:\/\/en.wikipedia.org\/wiki\/Emacs\" target=_blank mce_href=\"http:\/\/en.wikipedia.org\/wiki\/Emacs\">Emacs<\/A> etc.&nbsp; Go crazy and share share share.<\/P>\n<P>Enjoy!<\/P>\n<P>Jeffrey Snover [MSFT] <BR>Windows Management Partner Architect <BR>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <A href=\"http:\/\/blogs.msdn.com\/PowerShell\" mce_href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/A> <BR>Visit the Windows PowerShell ScriptCenter at:&nbsp; <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" mce_href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let me combine a couple of things.&nbsp; 1) When I posted my PowerShell_ISE profile, I included a function Goto-Line that is now builtin to the ISE so I reposted the blog with this function commented out. I commented it out instead of deleting it because I wanted to provide an example of how to do [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137,210,260],"class_list":["post-17804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-ctp3","tag-jeffrey-snover","tag-powershell-ise"],"acf":[],"blog_post_summary":"<p>Let me combine a couple of things.&nbsp; 1) When I posted my PowerShell_ISE profile, I included a function Goto-Line that is now builtin to the ISE so I reposted the blog with this function commented out. I commented it out instead of deleting it because I wanted to provide an example of how to do [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=17804"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/17804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=17804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=17804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=17804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}