Explain HTML formatting in PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

HTML is another form of output in PowerShell. It is the rich form of output and you can use various CSS styles to make the output more interactive. We will use the Convertto-HTML cmdlet to convert the output in the HTML format.

Here is the Syntax of the Convertto-HTML cmdlet.

Example

ConvertTo-Html
    [-InputObject <PSObject>]
    [[-Property] <Object[]>]
    [[-Body] <String[]>]
    [[-Head] <String[]>]
    [[-Title] <String>]
    [-As <String>]
    [-CssUri <Uri>]
    [-PostContent <String[]>]
    [-PreContent <String[]>]
    [-Meta <Hashtable>]
    [-Charset <String>]
    [-Transitional]
    [<CommonParameters>]

ConvertTo-Html
    [-InputObject <PSObject>]
    [[-Property] <Object[]>]
    [-As <String>]
    [-Fragment]
    [-PostContent <String[]>]
    [-PreContent <String[]>]
    [<CommonParameters>]

Let's take one simple example of Convertto-HTML with InputObject parameter.

ConvertTo-Html -InputObject (Get-Date)

The output will be on the same console.

PS C:\WINDOWS\system32> ConvertTo-Html -InputObject (Get-Date)
<!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/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>DisplayHint</th><th>DateTime</th><th>Date</th><th>Day</th><th>DayOfWeek</th><th>DayOfYear</th><th>Hour</th><th>Kind<th><th>Millisecond</th><
th>Minute</th><th>Month</th><th>Second</th><th>Ticks</th><th>TimeOfDay</th><th>Year</th></tr>
<tr><td>DateTime</td><td>17 June 2020 08:33:06</td><td>17-06-2020 00:00:00</td><td>17</td><td>Wednesday</td><td>169</td><td>8</td><td>Local</td><td>8
29</td><td>33</td><td>6</td><td>6</td><td>637279795868299280</td><td>08:33:06.8299280</td><td>2020</td></tr>
</table>
</body></html>

So, you need to redirect the output in the .html extension.

ConvertTo-Html -InputObject (Get-Date) > C:\temp\dateoutput.html

Output

Here, You can also use the Pipeline as the Input Object. An example is shown below.

Below we will convert Get-Service output to HTML format.

Get-Service | Select Name, Status, StartType | ConvertTo-Html | Out-File Servicesoutput.html

When you check the output in a file called ServicesOutput.html, HTML file will be as below.

Output

You can also check the content of the HTML by right click and edit with Notepad, Notepad++ or any compatible editor.

The HTML file will be as below in the editor. You can see that the title “HTML Table”, it is added automatically. You can change the title in the editor but it won’t help as it will be overwritten to default every time you run the script.

<!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/><col/><col/></colgroup>
<tr><th>Name</th><th>Status</th><th>StartType</th></tr>
<tr><td>AarSvc_69f5c</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>AdobeARMservice</td><td>Running</td><td>Automatic</td></tr>
<tr><td>AdobeFlashPlayerUpdateSvc</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>AJRouter</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>ALG</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>AppIDSvc</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>Appinfo</td><td>Running</td<td>Manual</td></tr>
<tr><td>AppMgmt</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>AppReadiness</td><td>Stopped</td><td>Manual</td></tr>
<tr><td>AppVClient</td><td>Stopped</td><td>Disabled</td></tr>

So to add the title in the script, you need to use the –Title parameter. For example,

Get-Service | Select Name, Status, StartType | ConvertTo-Html -Title "Services Output" | Out-File Servicesoutput.html

When you check the output, you will can see the title.

And the HTML file of the above output in the editor, you can see the new title now.

Output

<!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>Services Output</title>
</head><body>
<table>

Instead of using Pipeline properties in Get-Services output, you can also use the Convertto-HTML properties to select the output from the previous command.

For example,

Get-Service  | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" | Out-File Servicesoutput.html

It will yield the same result as the above HTML output.

If you need to add the description before the result, you need to use –PreContent parameter. For example,

Get-Service  | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "Services Output" | Out-File Servicesoutput.html

Output

You can twik the precontent with the HTML headers. For example,

Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "<h1><center>Services Output</center></h1>" | Out-File Servicesoutput.html

Output

In the above example, we have put the header and center alignment for the PreContent. Similarly, you can use the PostContent to display the title at the bottom. For example,

Example

Get-Service | ConvertTo-Html -Property Name, Status, Starttype -Title "Services Output" -PreContent "<h1><center>Services Output</center></h1>" -PostContent "<div align=right><b>From: PowerShell Scripter</b></div>" | Out-File Servicesoutput.html

raja
Published on 11-Nov-2020 16:13:02
Advertisements