Powershell html table formatting for disk space report


I’m using below code to get disk space information for servers along with custom info in html format…

My question is how do I use html bgcolor property in ‘Check-DiskSta’ function for ‘C_PercentFreeGB’ and ‘D_PercentFreeGB’ column with red for <10% and orange for <15% free disk space value.


$header = "

DiskSpace Report

Daily Health Check $strDate


#Disk information for all critical servers
Function Check-DiskStat()

$csvPath = ‘.\ServerInventory.csv’
$DiskReport = foreach ($Server in (Import-Csv $csvPath)) {

    New-Object -TypeName PSObject -Property @{
        ServerName    = $Server.ServerName 
        Application   = $Server.ServiceType
        ServiceName   = $Server.ServiceName
        Environment   = $Server.Env
        C_SizeGB= $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'C:'} | Select @{Name="Size";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}).Size
        C_FreeSpaceGB= $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'C:'} | Select @{Name="Size";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}).Size
        C_PercentFreeGB=  $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'C:'} | Select @{Name="Size";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}).Size

        D_SizeGB= $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'D:'} | Select @{Name="Size";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}).Size
        D_FreeSpaceGB= $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'D:'} | Select @{Name="Size";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}).Size
        D_PercentFreeGB=  $(Get-WmiObject Win32_logicaldisk -Filter "DriveType = 3" -ComputerName $Server.ServerName | 
                        ?{$_.deviceid -eq 'D:'} | Select @{Name="Size";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}}).Size
       }| select ServerName, Application, C_SizeGB, C_FreeSpaceGB, C_PercentFreeGB, D_SizeGB, D_FreeSpaceGB, D_PercentFreeGB  
Return, $DiskReport

$dskrpt = Check-DiskStat
$Diskinformation = $dskrpt| ConvertTo-Html -Fragment -PreContent ‘Disk Information’ |Out-String

ConvertTo-Html -Head $header -PostContent $Diskinformation, $funout2, $funout3 -CssUri ‘F:\MyDocs\MyScripts\DailyHealthCheck\out.css’ | out-file $Result

If you could please format your code, according to the E-Z instructions just above the posting textbox, that’d make it a ton easier to help. Thanks!

ConvertTo-HTML doesn’t really make possible what you want to do. Consider reading our free ebook, on the ebook menu, “Creating HTML Reports in PowerShell.” There’s an EnhancedHTML2 module in PowerShell Gallery which can do this, and the example in the ebook is also for disk space.

$objects = get-process 

$html = $objects | select-object name,id,handles |ConvertTo-Html -Fragment
$xml = [xml]$html
$rows = $xml.table.selectNodes('//tr')
for($i=1;$i -lt $rows.count; $i++){
    if([int]$value -lt 100){
       $attr.Value='background-color: red;'




	$a = ''
	$a = $a + "BODY{FONT-SIZE: 8pt; FONT-FAMILY: verdana}"
	$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
	$a = $a + "TH{border-width: 1px;padding: 4px;border-style: solid;border-color: black}"
	$a = $a + "TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black}"
	$a = $a + ""

$body = ConvertTo-Html -head $a -body $html -Title "table selective color" | Out-String

		$body |out-file color.htm