How to use PowerShell to create HTML from a Text File

Recently I was tasked with creating a Cluster Health Report for the Windows 2008 R2 Clusters. I was able to do this with PowerShell using the failovercluster cmdlets. This was outputted to a text file and emailed to the support for review daily.

To expand / enhance the report it was suggested to create it in HTML format. So back to PowerShell we go and low and behold we find the ConvertTo-Html cmdlet. Looking at the get-help and document one would think that using the command is straight forward, but in reality it is a bit more to it.

For example, we could pass

get-service | convertto-html -as LIST > c:\temp\services.htm

and get

Pretty simple, right? That’s what I thought, so my inclination was to feed it the text file to ConvertTo-Html and Get_content and I will have a finished product.

$SourceFile = "C:\Support\clustercheck.txt"
$TargetFile = "C:\Support\clustercheck.htm"

Get-Content $SourceFile | ConvertTo-Html | Out-File $TargetFile

No such dice, what I received was a bunch of garbage output. Upon further research found rather than just wrapping text in HTML, ConvertTo-Html will output  properties of objects to HTML. When reading the file using Get-Content you end up with an array of string objects. These objects are being passed to ConvertTo-Html through the pipe. The string objects contain properties. And these properties are being output to the HTML. In order to use ConvertTo-Html we will need to create objects that would contain text lines as properties.

So things just got a bit more complicated. What we have to do is read each line of the text file and create a PSObject and add-member for each of the lines, then run ConvertTo-HTML. What you end up with is this code.

$SourceFile = "C:\Support\clustercheck.txt"
$TargetFile = "C:\Support\clustercheck.htm"

$File = Get-Content $SourceFile
$FileLine = @()
Foreach ($Line in $File) {
 $MyObject = New-Object -TypeName PSObject
 Add-Member -InputObject $MyObject -Type NoteProperty -Name HealthCheck -Value $Line
 $FileLine += $MyObject
$FileLine | ConvertTo-Html -Property HealthCheck -body "<H2>Cluster Healthcheck</H2>" | Out-File $TargetFile

Now when it is ran we get what we were looking for in the first place. So no we have a start towards an HTML outputted report. So if we wanted to make the report look nicer we could pass formatting in the ConvertTo-Html cmdlet.

Using the above snippet as a shell you can use any text file to create html formatted reports in PowerShell.


5 Responses to “How to use PowerShell to create HTML from a Text File”

  1. Juwel says:

    This is a great Post,Thank you very much!
    I want to print the data as HTML table – how can I do that?

  2. mahantesh says:

    hi, i have text file which I would like to convert to html. the text file contains columns, i would like to convert this to html with columns displaying in table. below is sample of text in text file.
    Changeset Merged in Changeset Author Date
    ——— ——————- ———– ———
    179 407 aaaa 5/28/2014
    182 407 aaaa 5/28/2014
    203 407 aaad 5/28/2014

    • newlife007 says:

      Using get-content to read in the information then pipe it tp converttohtml is a way to achieve this. I don’t have any examples of how to do this but n a quick Google there are a few examples. Let me see if I can come up with an example for you.

  3. Jeff says:

    You code seems to work however the browser strips out any spacing in the line. For example if my line is blah blah 05.20.2013. The output in the browser is blah blah 05.2.2013. Viewing the source will render the proper format that is shown in the text file. So I guess if the text file has random spacing then the HTML will not be correctly displayed or interpreted by the browser.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>