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.
Olaf
February 23, 2023, 2:23pm
2
chirag,
Welcome to the forum.
You would need to parse the output and format each individual part of the output separately.
IMHO it is not worth the effort. If you want to have a visually nice output you may try to create an HTML report.
1 Like
tonyd
February 23, 2023, 10:34pm
3
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.
Olaf
February 24, 2023, 12:04am
4
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 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
tonyd
February 27, 2023, 4:48pm
6
Very cool and very much appreciated Doug
Thank you.
1 Like
Olaf
March 12, 2023, 11:59pm
7
Here is something new that might come in handy for you
… and if you like to take a look at the code …
function Out-Conditionalformatting {
<#
.SYNOPSIS
Colorize your PowerShell data output the way you want
.EXAMPLE
$data = ((Get-Process).Where({ $_.Company }) | Select-Object Company, Name, Handles, NPM, PM -First 15)
Out-Conditionalformatting $data {
if ($item.Handles -ge 500) {
$PSStyle.Background.Green
}
else {
$PSStyle.Background.Red
}
}
#>
param(
[scriptblock]$sb,
[Parameter(ValueFromPipeline)]
$targetData
)
This file has been truncated. show original
tonyd
March 13, 2023, 3:49pm
8
Thanks Olfa, much appreciated