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

9 months, 3 weeks ago.

  • Author
  • #105877

    Topics: 3
    Replies: 4
    Points: 0
    Rank: Member


    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.

  • #105889

    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major 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.

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

  • #105901

    Topics: 2
    Replies: 937
    Points: 1,629
    Helping Hand
    Rank: Community Hero

    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..


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

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort