How to change color of PSCustomObject key value

In my script, I have match Installed computer program with my text file, If the installed program is match with my taxt file then showing same program name in Installed-App column, if it is not match displaying “Not Installed”. I Want Hightlight “Not Installed” in RED color which is define in

$apps = [PSCustomObject]@{RequirApp=$requirapp; $systemapp=“Not Installed”}. Can you help me out anyone. Hear is my code

$appname  = Get-Content -path $env:USERPROFILE\Documents\Installed-Applications.txt

$INSTALLED = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$INSTALLED += Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
$INSTALLED += Get-ItemProperty HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
#$INSTALLED | ?{$_.DisplayName -ne $null} | Select-Object -Property DisplayName | out-file $env:USERPROFILE\Documents\Installed-Applications.txt
$sysapps = $INSTALLED | ?{$_.DisplayName -ne $null}

for($i=0; $i -lt $sysapps.Count; $i++)
{
    $installapp += $sysapps[$i].DisplayName
}

foreach($a in $appname)
{
        $requirapp = $a
          
        if($installapp -contains $requirapp.Trim())
        {
           
           $apps = [PSCustomObject]@{RequirApp=$requirapp; $systemapp=$requirapp} 
           
        }
        else
        {
            
            $apps = [PSCustomObject]@{RequirApp=$requirapp; $systemapp="Not Installed"}
            
        }  
        $apps
       
  }

I want to be such output.
NeedOutput

chirag,
Welcome to the forum. :wave:t4:

You would need to parse the output and format each individual part of the output separately.

IMHO it is not worth the effort. :man_shrugging:t4: If you want to have a visually nice output you may try to create an HTML report.

1 Like

Although I agree with Olaf, and his proposal is exactly what I do to obtain what you ask, I am curious how this might be done (and don’t have the time to mess with it). So, if you do find a way, please post your results if you don’t mind.

I haven’t played with it yet because I actually don’t care about color in the console but you may try to tinker something with PowerShell version 7.x and the $PStyle stuff …

Just for you Tony :smiley: You can use escape sequences

$ESC = [char]0x1b
$red = "$ESC[91m{0}$ESC[0m"
$green = "$ESC[92m{0}$ESC[0m"

$servicelist = Get-Service

$colorprops = 'Name','StartType',@{n='Status';e={
    if($_.starttype -ne 'Disabled'){
        if($_.status -ne 'Running'){
            $red -f $_.Status
        }
        else{
            $green -f $_.Status
        }
    }
    else{
        $_.Status
    }
}}

$servicelist | Select-Object $colorprops

Can find some that set the background instead

$ESC = [char]0x1b
$red = "$ESC[41m{0}$ESC[0m"
$green = "$ESC[42m{0}$ESC[0m"

$servicelist = Get-Service

$colorprops = 'Name','StartType',@{n='Status';e={
    if($_.starttype -ne 'Disabled'){
        if($_.status -ne 'Running'){
            $red -f $_.Status
        }
        else{
            $green -f $_.Status
        }
    }
    else{
        $_.Status
    }
}}

$servicelist | Select-Object $colorprops

Note this does not work in the ISE, but the console works fine. Even 5.1!

I used this to find the ones I was looking for

$ESC = [char]0x1b

1..100  | Foreach-Object{
    Write-Host Number is $_
    "text is $ESC[${_}mThis Color$ESC[0m"
}
2 Likes

Very cool and very much appreciated Doug :slight_smile:

Thank you.

1 Like

Here is something new that might come in handy for you

… and if you like to take a look at the code …

Thanks Olfa, much appreciated :slight_smile: