Change the background and foreground colors on only one table entry


I am looking to change the background and foreground colors of only specific table entries, based on their value. What I am finding so far is that it changes the colors for everything. Here is what I have so far:

foreach($displayname in $InputObject){
         "Group Name" = $InputObject.displayname
         "Members" = if((($InputObject.members).count) -gt ((Get-UnifiedGroupLinks "gr-$($InputObject.displayname)" -LinkType members | select name).count))
             $bc = (Get-Host).UI.RawUI
             $Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'yellow')
             write-output ($InputObject.members).count
             $host.ui.RawUI.BackgroundColor = $bc
         "Prod Group" = Get-UnifiedGroup "gr-$($InputObject.displayname)" | select -ExpandProperty displayname
         "Prod Members" = (Get-UnifiedGroupLinks "gr-$($InputObject.displayname)" -LinkType members | select name).count

So ideally, with the following output:

Group Name           Members Prod Group              Prod Members 
----------           ------- ----------              ------------ 
testgroup1                 5 gr-testgroup1                      5                                    
testgroup2                 5 gr-testgroup2                      4

The 5 in testgroup2 only would be changed, but the rest would be standard colors. With the code above, all the text changes.

Thanks for your consideration.

Manipulating colors in the shell are limited. $host is the host, the entire shell you are in. There is also:

PS C:\Users\Rob> Write-Host "This is a red string" -ForegroundColor Red
This is a red string

If you are wanting to change the colors of cells or rows, you’re better off working in HTML as your output where you can do just about anything that you want.

There is also a free e-book on ‘Creating HTML Reports in PowerShell’ in the ebooks link above

It is possible colorize console output. I do this regularly. Yet, understand that different environments can cause challenges.

Format the output table colors in PowerShell host with/without conditional colorful formatting.


and this…


It’s also been asked for via uservoice on the MS PS Github as a native thing.


See also this discussion which also points to the above.


However, going the HTML route, be sure to look at the ‘EnhancedHTML module’ to help you out with this use case.


See this thread as well…