0

I have a powershell loop that builds a table. In one of the columns, I would like to compare the number and if it's below a certain amount, display just that item in red. How can I do this? I'm building and displaying my table this way:

$mytab = ()
foreach ($1 in $all)
{
  $ltr = $1.letter
  $lbl = $1.label
  $num = $1.number
  $mytab += new-object psobject -property @{
      letter = $($ltr);
      label = $($lbl);
      number = $($num);
  }
}
$mytab | ft letter,label,number

So let's say in one iteration of my foreach loop, ($num -lt 20) is true. I want that specific instance of $num to display in the output of $mytab in red while the other columns' values are normal text color.

I can't think of any way to make write-host do this in this case, and

Am I asking too much? I realize I could probably iterate through the array, and rebuild the table a second time, but I'm hoping there's a much easier way that I'm just not aware of.

Thanks.

ETA: If not possible, I'd rather just use special characters to highlight the text than color the whole row. Right now, I'm handling this with an if statement to wrap the text in !!! characters before feeding it into the array.

2
  • 1
    Possible duplicate of How to colorise Powershell output of format-table Commented Dec 20, 2016 at 20:09
  • Thanks. Didn't find that one when I tried searching. Still, I don't want the whole row colored, only the one "cell." If the second response in the link is accurate, then I'll just stick with wrapping the text in some form of "look at me!" text. Right now I'm settling for !!!19%!!!. Commented Dec 21, 2016 at 20:05

1 Answer 1

1

Check out these links:

https://www.reddit.com/r/PowerShell/comments/5pdepn/writecolor_multiple_colors_on_a_single_line/

https://www.powershellgallery.com/packages/Communary.ConsoleExtensions/1.0.52/Content/Functions%5CWrite-RGB.ps1

They might help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.