In my earlier article where I pointed out some of my favorite features of Windows PowerShell V3 I noted one failing of the new Windows PowerShell ISE – the inability to copy text as HTML so that I can easily paste that HTML into my blog posts. I hate complaining about things if it’s in my ability to fix the issue so I decided to create a custom add-on for the ISE which would add this feature.

Creating the add-on itself was actually pretty easy – I spent more time trying to get the stupid installer to work so that it would properly update the Microsoft.PowerShellISE_profile.ps1 script file than I did actually writing the add-on. The way you create most add-on applications for the ISE is by simply using the $psISE variable which is of type Microsoft.PowerShell.Host.ISE.ObjectModelRoot. Using this you can get access to various aspects of the ISE including the add-on menu and all of the tabs and editors. For most add-on applications you could do everything in a script file and keep things real simple, however, for this one I needed a custom assembly as I had a fair bit of code to convert the RTF output generated by the ISE into HTML. The other problem I had was that, unfortunately, Microsoft decided to make the methods which retrieve the text as RTF text internal so I had to use some reflection in order to get the actual RTF text.

Once I had my assembly created all I had to do was put together a simple script that loaded the assembly and created an instance of my main class – I could then add an item that would execute my main method to the Add-ons menu. Here’s a snippet of that script (generated using the add-on):

1Add-Type -Path "$installPath\Falchion.PowerShell.CopyAsHtmlAddOn.dll"
3$copyAsHtmlAddOn = New-Object Falchion.PowerShell.CopyAsHtmlAddOn.CopyAsHtml
5$copyAsHtmlAddOn.HostObject = $psISE
7$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Copy as HTML", { $copyAsHtmlAddOn.Copy() }, "CTRL+SHIFT+C") | Out-Null

In the first three lines I’m simply registering my type, creating a new instance of my CopyAsHtml class, and then setting the HostObject property of the class to the $psISE object so that the methods within the object can access it. The last line is what wires it all up. I’m using the CurrentPowerShellTab property to access the AddOnsMenu property and then the Submenus property; I then call the Add method providing the label for my menu item, the script block to execute when the menu item is triggered, and finally, the keyboard shortcut that you can use to trigger the menu item. All of this code is placed in a script file which is loaded by the Microsoft.PowerShellISE_profile.ps1 file (the installer updates the all users version of this file). Whenever the menu item is triggered it will run the code in the script block ($copyAsHtmlAddOn.Copy()) which will then take any selected text in the editor and copy it into the clipboard as HTML.

Note: Because the ISE is executing this code in the console pane this won’t work for text selected in the console pane as the selection is lost when the code is executed (so you end up getting a blank line of HTML in the clipboard).

You can download the installer from here. The installation is pretty straightforward so I won’t bother with any walkthrough or anything. The only thing worth noting is that there are situations in which the installer may not be able to update the profile file (or may not update it properly). During installation, the installer tries to update the PowerShell Profile file located at %windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1 AND %windir%\SYSWOW64\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1. This profile applies to all users, and it executes the script Falchion.PowerShell.CopyAsHtmlAddOn.ps1 located in the add-on installation folder during the ISE startup. As noted earlier, this script contains instructions to add the Copy as HTML add-on as an ISE Add-On.

Also, by default, Windows PowerShell ISE execution policy provides restricted access. If you are running Windows PowerShell ISE under restricted access, you will not be able to run scripts, and the Copy as HTML add-on will not appear in the Add-ons menu. You need to lower your execution policy. Once installed, start Windows PowerShell ISE and notice that a Copy as HTML option is now available in the Add-ons menu. Select text in the editor and click that option to perform the copy operation (or press CTRL+SHIFT+C).


Version:1.0Date published:10/23/2012
File nameSize
Falchion.PowerShell.CopyAsHtmlAddOn.zip422 KBDownload