How to use CSS style in PowerShell HTML Output?

Cascading Style Sheets (CSS) in general used for formatting HTML with styles. It describes how the HTML elements should be displayed.
Once we get the output from the Convertto-HTML command, we can use the CSS style to make the output more stylish.
Consider we have the below example for converting the services output table to the HTML.
Get-Service | Select Name, DisplayName, Status, StartType | ConvertTo-Html -Title "Services" -PreContent "<h1>Services Output</h1>" | Out-File Servicesoutput.html
The output is simple for the above command in HTML.
There are multiple ways to add the CSS style in the above HTML file so the file output becomes more stylish. We will use the most common ways to add the CSS style.
Header
Header
CSSURI
CSSURI
Header Method.
Header Method.
Convertto-HTML cmdlet supports the -Head property. We can provide header for the HTML along with <style></style> tag so that the PowerShell can understand the style. For example,
Convertto-HTML cmdlet supports the -Head property. We can provide header for the HTML along with <style></style> tag so that the PowerShell can understand the style. For example,
$head = @"

<style>
    body
  {
      background-color: Gainsboro;
  }

    table, th, td{
      border: 1px solid;
    }

    h1{
        background-color:Tomato;
        color:white;
        text-align: center;
    }
</style>
"@

Get-Service | Select Name, DisplayName, Status, StartType | ConvertTo-Html -Title "Services" -PreContent "<h1>Services Output</h1>" -Head $head | Out-File Servicesoutput.html
In the above script, we have used $head as an array so we can cover the entire CSS script in between <style></style> tags.
The first element is the HTML body and we are providing Gainsboro for the background color of the web page. Next, we are table, th (table header) and td (table data) to cover its borders and at the last, we used -Precontent property between <h1></h1> tag and at the same time we have mentioned h1 tag inside the CSS file so we provide different attributes as per our style requirement.
Similarly, you can use the CSS reference guide and play with multiple styles. This method is simple because you don't need to provide the separate CSS file to other users when you share the script but the problem is that the script becomes sometimes lengthy when you add more styles. To overcome this problem, we can provide the external CSS file to the Convertto-HTML cmdlet as shown in the second method.
Cssuri method (External CSV)
Cssuri method (External CSV)
In this method, instead of providing Header property to Convertto-HTML cmdlet, we can use cssuri property to link the external CSS file. CSS file will remain same as mentioned in the first method, except <style></style> parameter won't be added. First, we will check the CSS file saved with the name Style.css and then we will see the script to link the external CSS file.
In this method, instead of providing Header property to Convertto-HTML cmdlet, we can use cssuri property to link the external CSS file. CSS file will remain same as mentioned in the first method, except <style></style> parameter won't be added. First, we will check the CSS file saved with the name Style.css and then we will see the script to link the external CSS file.
body
  {
      background-color: Gainsboro;
  }

table, th, td{
  border: 1px ridge;
}

h1{
    background-color:Tomato;
    color:white;
    text-align: center;
}

Get-Service | Select Name, DisplayName, Status, StartType | ConvertTo-Html -Title "Services" -PreContent "Services Output" -CssUri .\style.css | Out-File Servicesoutput.html
As mentioned earlier in separate CSS file, you don't need to mention the <style></style> tag. In the above command, we have provided the path of the CSS file to -Cssuri cmdlet. Here, script and the CSS file both are at the same location. If the CSS file resides on a different location, you need to provide the full path of the CSS file. The output remains the same as shown in the first example.