How to colorize PowerShell output from Format-Table

I am trying to color the column RAM in red if the value is greater than 100 MB:

Get-Process | Format-Table @{ Label = "PID"; Expression={$_.Id}}, @{ Label = "Name"; Expression={$_.Name}}, @{ Label = "RAM (MB)"; Expression={[System.Math]::Round($_.WS/1MB, 1)}}, @{ Label = "Responding"; Expression={$_.Responding}} 

Enter image description here

I am trying to use Write-Host -nonewline , but the result is incorrect.

 Get-Process | Format-Table @{ Label = "PID"; Expression={$_.Id}}, @{ Label = "Name"; Expression={$_.Name}}, @{ Label = "RAM (MB)"; Expression={write-host -NoNewline $([System.Math]::Round($_.WS/1MB, 1)) -ForegroundColor red}}, @{ Label = "Responding"; Expression={ write-host -NoNewline $_.Responding -fore red}} 

Enter image description here

+11
colors powershell console
source share
6 answers

You can colorize a string using a regular expression ...

 filter colorize-row{ Get-Process | Select-Object Id, Name, WS, Responding | foreach { # Print 'red' row if WS greater than 100 MB if([System.Math]::Round($_.WS/1MB,1) -match "^([0-9]|[0-9][0-9]|[1-9][0-9]?$|^100$)$"){ [console]::ForegroundColor="white"; $_; } else { [console]::ForegroundColor="red"; $_; } } } colorize-row 

Exit:

Enter image description here

+10
source share

Starting with PowerShell 5.1 or later, you can use VT escape sequences to add colors to a single column, but only if your console supports VT escape (e.g. Windows 10 Fall Creators Update, Linux or Mac, but not Windows 8 with console emulator, for example ConEmu).

Here is an example that has the formatting specified in the expression, although the same could be used in the ps1xml file:

 dir -Exclude *.xml $pshome | Format-Table Mode,@{ Label = "Name" Expression = { switch ($_.Extension) { '.exe' { $color = "93"; break } '.ps1xml' { $color = '32'; break } '.dll' { $color = "35"; break } default { $color = "0" } } $e = [char]27 "$e[${color}m$($_.Name)${e}[0m" } },Length 

And the result, note that the column width looks good, there are no extra spaces from escape characters.

Screenshot with output with colored names

+20
source share

The accepted answer is incorrect, it is possible to colorize the columns. The solution to get conditional column colors is to use Write-PSObject .

Here are some great examples with documented code and explanations.

From the above resource:

 Write-PSObject $servers -MatchMethod Exact -Column "Manufacture" -Value "HP" -ValueForeColor Yellow -ValueBackColor Red -RowForeColor White -RowBackColor Blue; 

enter image description here

I discovered this through a GitHub issue, to add color formatting to the Format-Table , which seems to be a feature that PowerShell developers would like to add at some point.

+14
source share

The quick answer: you cannot. You can use Write-Host with colors, but there is no "output" to send to format-table.

The "output" from Write-Host is a side effect that sends data directly to the console, and does not return it to the caller, as a standard function.

In conjunction with the comment by @David Martin, there is a link with an interesting matching function with a color-format pattern.

+4
source share

This should be a comment on the OptimusPrimus answer.

Note that you cannot use powershell_ise for this.

0
source share

You can install the PsWrite module

Color recording enter image description here

it also has other smart features like

Write-logstep enter image description here

Write-ProgressbarreColor enter image description here

0
source share

All Articles