Unfortunatly I did not find a way to colorize a table without using Write-Host. This would be very awesome and would safe a lot of code.

The problem with outputting tekst to the console with Write-Host is that you can’t use the automatic width of the columns. To fix that I calculated the width of every column I want to output to the screen and looping a total amount of characters to the screen. What you see is not a table, but plain Write-Host using spaces!

The result I have accomplished:

Code:

#Get Some data to work with
$PSServices = Get-Service | select *

##########################################################
#
# DataTable
#
##########################################################

# Create a DataTable object with the name "Services"
$DataTable = New-Object -TypeName System.Data.DataTable "Services"

# Add columns (column names) to the DataTable
# The Out-Null is not necessary but prevents some output printed to the screen.
$DataTable.Columns.Add("DisplayName") | Out-Null
$DataTable.Columns.Add("Status") | Out-Null
$DataTable.Columns.Add("StartType") | Out-Null

foreach($Service in $PSServices) 
{
    # Create a new row
    $Row = $DataTable.NewRow()

    # Add data to the row
    $row["DisplayName"] = $Service.DisplayName
    $row["Status"] = $Service.Status
    $row["StartType"] = $Service.StartType

    # Add the row to the DataTable
    $DataTable.Rows.Add($row)
}

$DataTable

##########################################################
#
# Colorize dataTable with spacing
#
##########################################################

# First measure the maximum width of all the columns.
# I use 3 columns.
$PSObjectArray | %{ [array]$array04 += $_.DisplayName} 
$Column01_Width = ($array01 | measure-object -Maximum -Property Length).Maximum
$PSObjectArray | %{ [array]$array02 += ($_.StartType).toString()} 
$Column02_Width = ($array02 | measure-object -Maximum -Property Length).Maximum
$PSObjectArray | %{ [array]$array03 += ($_.Status).toString()} 
$Column03_Width = ($array03 | measure-object -Maximum -Property Length).Maximum

# Function to write spaces to the screen without going to a new line
Function Space($i){for ($r = 0; $r -le $i; $r++) {Write-Host " " -NoNewline}}

# Loop through the dataTable
foreach($row in $DataTable)
{    
    $Column01 = [string]$row.DisplayName
    $Column02 = [string]$row.StartType
    $Column03 = [string]$row.Status   
    ###########
    # Column01
    ###########
    for ($i = 0; $i -le ($Column01).Length; $i++)
    { 
        Write-Host ($Column01)[$i] -NoNewline
    }
    Space($Column01_Width - (($Column01).Length))
    ###########
    # Column02
    ###########
    for ($i = 0; $i -le ($Column02).Length; $i++)
    { 
        Write-Host ($Column02)[$i] -NoNewline
    }
    Space($Column02_Width - (($Column02).Length) + 1)
    ###########
    # Column03
    ###########
    for ($i = 0; $i -le ($Column03).Length; $i++)
    { 
        if($Column03 -eq "Stopped") {
            Write-Host ($Column03)[$i] -NoNewline -ForegroundColor Red
        } else {
            Write-Host ($Column03)[$i] -NoNewline -ForegroundColor Green
        }
    }
    Space($Column03_Width - (($Column03).Length))
    ###########
    # End...
    ###########
    Write-Host ""
}

I hope you can use this example for something usefull. Personally this cost me a lot of time to figure out 🙂