How to convert string in a variable to html table

T Crha 381 Reputation points
2023-05-04T12:26:13.83+00:00

Hello everyone,

I am generating a report that I would like to send in some pretty and readable format. So basically, all the steps report is performing lead to a variable $report being filled in with a string like this:

User 123 is a O365 licensed user with access to O365 applications.
User 456 is a O365 licensed user with access to O365 applications.
User 789 is a O365 licensed user with access to O365 applications.
User abc is a O365 licensed user with access to O365 applications.
User def is a O365 licensed user with access to O365 applications.
User ghi is a O365 licensed user with access to O365 applications.

But when use the ConvertTo-Html utility, I get only this (similar result when attempting to Export-Csv):

$report | convertto-html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>134</td></tr>
<tr><td>137</td></tr>
<tr><td>138</td></tr>
<tr><td>135</td></tr>
<tr><td>134</td></tr>
<tr><td>135</td></tr>
</table>
</body></html>

Can anyone help me to format the output so it is in html format, and all the data is visible/readable?
How do I create a powershell object out of this so it is "convertible" to output formats like html/csv/json etc?

Many thanks,

Tomas the Noob

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,288 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 42,926 Reputation points
    2023-05-04T12:32:34.59+00:00

  2. Rich Matheisen 45,671 Reputation points
    2023-05-04T15:42:52.2666667+00:00

    Here's an example of converting your array into 1) simple paragraphs and 2) into a table.

    $report =   "User 123 is a O365 licensed user with access to O365 applications.",
                "User 456 is a O365 licensed user with access to O365 applications.",
                "User 789 is a O365 licensed user with access to O365 applications.",
                "User abc is a O365 licensed user with access to O365 applications.",
                "User def is a O365 licensed user with access to O365 applications.",
                "User ghi is a O365 licensed user with access to O365 applications."
    
    $freport = ForEach ($l in $report){
        $l = "<p>{0}</p>" -f $l     # each item becomes a paragraph
        $l
    }
    convertto-html -body $freport | out-file C:\junk\freport.html
    
    [array]$treport = "<table>","<colgroup><col/></colgroup>","<tr><th>Licenses</th></tr>"
    ForEach ($l in $report){
        $treport += "<tr><td>{0}</td></tr>" -f $l   # each item becomes a table entry
    }
    $treport += "</table>"
    ConvertTo-HTML -Head "<title>O365 users</title>" -body $treport | out-file c:\junk\treport.html
    
    0 comments No comments