Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

Format-Table with row colors in PowerShell

How to run Format-Table in PowerShell and format output as table with color logic driven from cell value. Full code below. Cheers

VIDEO

SOURCE CODE

# List process and format as table
$proc = Get-Process
$proc | Format-Table @{
    # Add column "Name" to table
    Label = "Name"
    Expression = {
        if ($_.Name -eq "chrome") {
            # Format row color Green when Name is "chrome"
            $color = "32" # Green
        } else {
            # Format row color Red when Name is not "chrome"
            $color = "31" # Red
        }
        # Character 27 is ESC
        $e = [char]27
        # Format row with name and color
        "$e[${color}m$($_.Name)$e[0m"
    }
}, PID, CPU, PM, WS, VM, NPM, Path -AutoSize

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲