How to change color of PSCustomObject key value

chirag darji 136 Reputation points
2023-02-23T09:40:03.19+00:00

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
  }

My script output I want such output

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,291 Reputation points
    2023-02-23T14:53:31.9433333+00:00

    See https://stackoverflow.com/questions/62617181/how-to-use-hex-codes-for-color-in-write-host

    https://wenijinew.medium.com/powershell-set-colorful-customized-prompt-c26ba496491

    Note that this will not work in Powershell_ISE.

    $apps = @()
    $systemapp = "This is systemapp"
    
    function to_yellow ($msg) {
       "$([char]0x1b)[93m$msg$([char]0x1b)[0m"
    }
    
    function to_green ($msg) {
    	 "$([char]0x1b)[32m$msg$([char]0x1b)[0m"
    }
    
    function to_red ($msg) {
    	 "$([char]0x1b)[91m$msg$([char]0x1b)[0m"
    }
    
    ""
    "This is in " + (to_green "green") + ", and this is in " + (to_yellow "yellow")
    ""
    $requirapp = "Notepad"
    $apps += [PSCustomObject]@{RequirApp=$requirapp; $systemapp= (to_green "Installed")}
    
    $requirapp = "Lotus Notes"
    $apps += [PSCustomObject]@{RequirApp=$requirapp; $systemapp= (to_red "Not Installed")}
    
    $apps
    
    

    User's image

    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Limitless Technology 44,746 Reputation points
    2023-02-23T16:44:17.2833333+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query

    According to powershellnut on this thread https://www.reddit.com/r/PowerShell/comments/o7vkr0/change_output_colors_on_pscustomobject_based_on/

    You will need to do 3 things.

    Add a type to your pscustomboject either when you create it or doing the follow

    $final.psobject.TypeNames.Insert(0,"myobject")

    Create a custom format file with a file extension of .ps1xml for this object

    Here is an example file I wrote for color text on certain properties - https://github.com/MrPig91/SysAdminTools/blob/main/SysAdminTools/Formats/LoggedInUser.ps1xml

    Update the formats for your powershell session by running the following

    Update-FormatData -AppendPath C:\formatfile.ps1xml

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. chirag darji 136 Reputation points
    2023-02-28T14:55:09.53+00:00

    Hi MotoX80,

    Thanks for the answer, this is worked. But when I export output to HTML, showing special character instead of value in HTML.

    powershelloutput

    HTMLoutput

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.