{"id":4651,"date":"2009-01-12T21:02:00","date_gmt":"2009-01-12T21:02:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/12\/how-to-copy-colorized-script-from-powershell-ise\/"},"modified":"2019-02-18T13:12:53","modified_gmt":"2019-02-18T20:12:53","slug":"how-to-copy-colorized-script-from-powershell-ise","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/how-to-copy-colorized-script-from-powershell-ise\/","title":{"rendered":"How to copy colorized script from PowerShell ISE"},"content":{"rendered":"<p><strong>UPDATED Feb-03-2009:<\/strong><\/p>\n<p>Lee Holmes has posted an <a href=\"http:\/\/www.leeholmes.com\/blog\/MorePowerShellSyntaxHighlighting.aspx\">updated version of the script<\/a> which does a much better job of HTML copy-pasting. It can also produce line numbers and has a number of other improvements.<\/p>\n<p>&#160;<\/p>\n<p>In the previous article we demonstrated how to use <a href=\"http:\/\/blogs.msdn.com\/powershell\/attachment\/9306712.ashx\">Console-Copy<\/a> script to make a colorized copy of console screen and place it in the system clipboard. Now what about doing the same thing with these nice looking colorized scripts that you can see in PowerShell ISE? Direct copy with Ctrl-C will copy just text without colorization. But the cool thing about PowerShell ISE is its great extensibility, which means that when you need a feature \u2013 you just script it! Let\u2019s follow this path and see what we can learn on the way.<\/p>\n<p>Let\u2019s start from end and figure out how we attach a script to PowerShell ISE user interface. In the example below we create a new menu item which will be displayed as <strong>Copy Script<\/strong> menu command under <strong>Custom<\/strong> menu group. Clicking on this menu will automatically execute the Copy-Script function. The third parameter sets the hot key as you probably have already guessed. If you don\u2019t want any hot key then just set its value to $null.<\/p>\n<p style=\"font-size: 10pt;margin: 0in 10pt;font-family: lucida console\"><span style=\"color: #ff4500\">$psise<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">CustomMenu<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Submenus<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Add<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">&quot;Copy Script&quot;<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #0000ff\">Copy-Script<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8b0000\">&quot;Shift+Ctrl+S&quot;<\/span><span style=\"color: #000000\">)<\/span>     <\/p>\n<p>Now to the script itself. First, you should check whether there is any script window opened at all.<\/p>\n<p style=\"font-size: 10pt;margin: 0in 10pt;font-family: lucida console\"><span style=\"color: #00008b\">function<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8a2be2\">Copy-Script<\/span>     <br \/><span style=\"color: #000000\">{<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #00008b\">if<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #a9a9a9\">-not<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$psise<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">CurrentOpenedFile<\/span><span style=\"color: #000000\">)<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #000000\">{<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span><span style=\"color: #0000ff\">Write-Error<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8b0000\">&#8216;No script is available for copying.&#8217;<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/span><span style=\"color: #00008b\">return<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #000000\">}<\/span><\/p>\n<p>If there is a script window opened then let\u2019s get the text from the editor and do the parsing.<\/p>\n<p style=\"font-size: 10pt;margin: 0in 10pt;font-family: lucida console\"><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$text<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$psise<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">CurrentOpenedFile<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Editor<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Text<\/span>     <\/p>\n<p><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #00008b\">trap<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #00008b\">break<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #000000\">}<\/span>     <\/p>\n<p><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #006400\"># Do syntax parsing.<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$errors<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$null<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$tokens<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #008080\">[system.management.automation.psparser]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">Tokenize<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$Text<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #008080\">[ref]<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$errors<\/span><span style=\"color: #000000\">)<\/span>     <\/p>\n<p>Parser can throw, that\u2019s why we need to set error trapping before we call it.<\/p>\n<p>We can ignore $errors for this particular task as the only thing we care is the set of tokens to use for colorization. Here is a list of tokens that PowerShell code can have:<\/p>\n<blockquote>\n<p><font face=\"Lucida Console\" color=\"#004080\" size=\"2\">PS D:\\&gt; [Enum]::GetNames([System.Management.Automation.PSTokenType])        <br \/>Unknown         <br \/>Command         <br \/>CommandParameter         <br \/>CommandArgument         <br \/>Number         <br \/>String         <br \/>Variable         <br \/>Member         <br \/>LoopLabel         <br \/>Attribute         <br \/>Type         <br \/>Operator         <br \/>GroupStart         <br \/>GroupEnd         <br \/>Keyword         <br \/>Comment         <br \/>StatementSeparator         <br \/>NewLine         <br \/>LineContinuation         <br \/>Position<\/font><\/p>\n<\/blockquote>\n<p>The parser will return a set of tokens which we can iterate through and generate colored RTF blocks and HTML spans using the same technique as described in <a id=\"ctl00___ctl00___ctl01___Results___postlist___EntryItems_ctl01_PostTitle\" href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2009\/01\/11\/colorized-capture-of-console-screen-in-html-and-rtf.aspx\"><u><font color=\"#800080\">Colorized capture of console screen in HTML and RTF.<\/font><\/u><\/a><\/p>\n<p>Once we are done, we should grab System.Windows.Clipboard and store our data, which will come in three flavors: UnicodeText, HTML and RTF:<\/p>\n<p style=\"font-size: 10pt;margin: 0in 10pt;font-family: lucida console\"><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$dataObject<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #a9a9a9\">=<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #8a2be2\">Windows.DataObject<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$dataObject<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">SetText\n<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008080\">[string]<\/span><span style=\"color: #ff4500\">$text<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #008080\">[Windows.TextDataFormat]<\/span><span style=\"color: #8b0000\">&quot;UnicodeText&quot;<\/span><span style=\"color: #000000\">)<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$dataObject<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">SetText<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008080\">[string]<\/span><span style=\"color: #ff4500\">$rtf<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #008080\">[Windows.TextDataFormat]<\/span><span style=\"color: #8b0000\">&quot;Rtf&quot;<\/span><span style=\"color: #000000\">)<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #ff4500\">$dataObject<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">SetText<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008080\">[string]<\/span><span style=\"color: #ff4500\">$html<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #008080\">[Windows.TextDataFormat]<\/span><span style=\"color: #8b0000\">&quot;Html&quot;<\/span><span style=\"color: #000000\">)<\/span>     <br \/><span style=\"color: #000000\">&#160;&#160;&#160; <\/span><span style=\"color: #008080\">[Windows.Clipboard]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">SetDataObject<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$dataObject<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #000000\">&#160;<\/span><span style=\"color: #ff4500\">$true<\/span><span style=\"color: #000000\">)<\/span><\/p>\n<p>Full script attached.<\/p>\n<p>Enjoy!    <br \/>Vladimir Averkin     <br \/>Windows PowerShell Team<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/09\/31\/20\/78\/Copy-Script.zip\">Copy-Script.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATED Feb-03-2009: Lee Holmes has posted an updated version of the script which does a much better job of HTML copy-pasting. It can also produce line numbers and has a number of other improvements. &#160; In the previous article we demonstrated how to use Console-Copy script to make a colorized copy of console screen and [&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":[],"class_list":["post-4651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>UPDATED Feb-03-2009: Lee Holmes has posted an updated version of the script which does a much better job of HTML copy-pasting. It can also produce line numbers and has a number of other improvements. &#160; In the previous article we demonstrated how to use Console-Copy script to make a colorized copy of console screen and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4651","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=4651"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4651\/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=4651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}