Change the background and foreground colors on only one table entry

Welcome Forums General PowerShell Q&A Change the background and foreground colors on only one table entry

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
5 months, 3 weeks ago.

  • Author
    Posts
  • #105877

    Participant
    Points: 0
    Rank: Member

    Hi,

    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){
         [pscustomobject]@{
             "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
                 }
                 else{
                       ($InputObject.members).count
                       }
             "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.

  • #105889

    Participant
    Points: 438
    Helping Hand
    Rank: Contributor

    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.

    https://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor

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

  • #105901

    Participant
    Points: 513
    Helping Hand
    Rank: Major Contributor

    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.

    'gallery.technet.microsoft.com/scriptcenter/Format-Table-Colors-in-e0a4beac'

    and this...

    'bgreco.net/powershell/format-color'

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

    'github.com/PowerShell/PowerShell/issues/3886'

    See also this discussion which also points to the above.

    'stackoverflow.com/questions/20705102/how-to-colorise-powershell-output-of-format-table'

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

    'powershellgallery.com/packages/EnhancedHTML2/2.1'

    See this thread as well..

    'powershell.org/forums/topic/conditional-formatting-in-psobject-output'

The topic ‘Change the background and foreground colors on only one table entry’ is closed to new replies.