Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Converting Text to HTML Revised

Posted on May 9, 2016

A few years ago I published a PowerShell function to convert text files into HTML listings. I thought it would be handy to convert scripts to HTML documents with line numbering and some formatting. Turns out someone actually used it! He had some questions about the function which led me to revisit it and realize there were some improvements to be made.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Part of the challenge with this project is that the function takes an existing text file and creates a much larger text file. On one hand this is to be expected because we are adding HTML to it.  But I wanted to find ways to keep the size down.

After some HTML style tweaking, I found a better way to preserve the  formatting without dramatically increasing the file size. I also added an option so that you can specify an alternate CSS file. If you don't use one, then the function will insert a default style into the head section. I also gave you the option to specify your own document title.

The revised script, which includes defining an alias, is now a gist on GitHub.

The function behaves much like ConvertTo-HTML in that it doesn't create an actual file. You still need to pipe the output to Out-File.  And you should use Out-File instead of the console redirection character (>) as that created a much larger version of the file.

Here's an example of a converted file using the default style sheet.

convertto-htmllisting2

If you want to create your own style sheet, be sure to use the setting white-space:pre; as this should retain document formatting. At least the best it can.

I hope you'll find this helpful. Enjoy.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Converting Text to HTML Revised”

  1. Bryan Price says:
    May 9, 2016 at 10:39 am

    Interesting. I have a pretty printer for Powershell files. I may steal some things from this. Especially the white-space:pre;! I currently emit a space and a   to preserve spacing (and expand tabs myself…). as that would save considerable space in a file. And reduce the complexity of my code, as well.

    And yeah, I probably should put my PrettyPrint-PS1.ps1 up somewhere like GitHub. :/

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d