Automatic formatting of code for posting on forums

Hello everyone,

As you may know from past experience or from reading the Forums Tips and Guidelines sticky, there are a couple of quirks with the forum software we're using here related to CODE tags and backtick characters. The forum software treats backticks as special characters, which is quite annoying if your code contains any of them, and you'll find that using PRE tags will give you much better results than CODE, particularly if someone tries to copy and paste your code into another window.

I've added a function to my PowerShell ISE profile which avoids these headaches by taking the code in the ISE's active window, escaping it in such a way that the forum will display it properly, wraps it in PRE tags (and also adds a blank comment line to the beginning and end of the code, to make copy / paste easier), and pipes the results to the clipboard. All I have to do is press F7 and then paste into the forum window. The function was even used to post itself here, which is about as good of a success indicator as I can think of.

The lines of this function may wrap in the blog post; there is a downloadable copy on the TechNet Gallery at

Edit: Looks like the handling of code formatting / HTML escaping on the blog posts has changed since it was originally posted, and the function posted here was displaying all sorts of double-escaped characters that it shouldn't have. Please use the TechNet download link to find the proper code.

About the Author

Dave Wyatt

Dave Wyatt is a Microsoft MVP (PowerShell) and a member of's Board of Directors.