Explain HTML formatting in PowerShell?

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 ]
    [[-Property] ]
    [[-Body] ]
    [[-Head] ]
    [[-Title] ]
    [-As ]
    [-CssUri ]
    [-PostContent ]
    [-PreContent ]
    [-Meta ]
    [-Charset ]
    [-Transitional]
    []

ConvertTo-Html
    [-InputObject ]
    [[-Property] ]
    [-As ]
    [-Fragment]
    [-PostContent ]
    [-PreContent ]
    []

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)



HTML TABLE<title>


Minute
DisplayHint DateTime Date Day DayOfWeek DayOfYear Hour Kind MillisecondMonth Second Ticks TimeOfDay Year
DateTime 17 June 2020 08:33:06 17-06-2020 00:00:00 17 Wednesday 169 8 Local 8 29 33 6 6 637279795868299280 08:33:06.8299280 2020

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.



HTML TABLE

Manual

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



Services Output

Name Status StartType
AarSvc_69f5c Stopped Manual
AdobeARMservice Running Automatic
AdobeFlashPlayerUpdateSvc Stopped Manual
AJRouter Stopped Manual
ALG Stopped Manual
AppIDSvc Stopped Manual
Appinfo Running
AppMgmt Stopped Manual
AppReadiness Stopped Manual
AppVClient Stopped Disabled

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 "

Services Output

" | 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 "

Services Output

" -PostContent "
From: PowerShell Scripter
" | Out-File Servicesoutput.html

Updated on: 2020-11-11T11:43:02+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements