Color-coding PowerShell operating system health reports

One of PowerShell’s greatest strengths is its ability to provide detailed information about operating system health. In my own environment, I have a number of different scripts that I periodically run in an effort to keep track of hardware health and resource usage. As great as it is to have such easy access to operating system health information through PowerShell, key details can sometimes be easy to miss. While you could address this problem by sorting the output or by creating filters that cause PowerShell to only display the things that need your attention, doing so might not always be desirable. Rather than hiding part of the output from view, it may sometimes be better to color-coding items of interest.

For the purposes of this article, I am going to create a simple script that examines memory usage on a per-process basis and color codes the processes that are using the most and the least memory. Keep in mind, though, that the basic technique that I am about to show you could be applied to virtually any PowerShell output.

The PowerShell cmdlet that displays a list of the processes that are running on a machine is Get-Process. If you want to customize the output to display only the process name, the process ID, and the working set size, you can use this command:

Get-Process | Select-Object ProcessName, ID, WorkingSet

operating system health
As you can see, PowerShell has produced a long list of processes. This particular machine is healthy, but if any of the processes were problematic, it might be tough to find those processes among all of the healthy ones simply because the list of processes is so long.

Color-coded operating system health reports: Step 1

The first step in creating color-coded output is to use a slightly different method to display the output that is shown above. Rather than calling the Get-Process cmdlet directly, let’s map the process list to a variable. That will make it a little bit easier to color code the output. Here is what such a command might look like:

$Processes = Get-Process

The next thing that we will need to do is to create a For Each loop. This loop will allow us to examine each process individually. For right now, we will simply design the loop to display the process name, process ID, and working set memory usage. Here is what the loop looks like.

$Processes = Get-Process
Write-Host ‘Process Name’`t `t ‘Process ID’ `t ‘Process Working Set’
ForEach ($Process in $Processes)
{
Write-Host -NoNewLine $Process.ProcessName
Write-Host -NoNewLine `t `t `t
Write-Host -NoNewLine $Process.ID
Write-Host -NoNewLine `t `t
Write-Host $Process.WorkingSet
}

As you can see, I am defining a variable named $Processes, and then creating a loop that steps through each individual process within the list of processes. For each process, the loop displays the process name, process ID and working set size. In case you are wondering, the `t that you see in several locations throughout the script tells PowerShell to insert a tab. Even with the tabs, the output is still kind of messy, as shown in the figure below. That’s OK, though. I plan to cover output formatting in a separate article. For now, I want to focus on color-coded output.

So, with that said, PowerShell allows you to specify both a foreground color and a background color as parameters of the Write-Host cmdlet. If, for example, you wanted to display white text on a red background, you could use a command like this:

Write-Host “Hello World” -ForegroundColor White -BackgroundColor Red

You can see an example of this below.

operating system health
Creating conditions

So now that we have created a loop that processes output items one at a time, and we have a way of switching colors, the only thing that is left to do is to create some conditions that will determine which colors will be used.

In the real world, you would obviously want to pick meaningful thresholds for use with your color codes. For my purposes, however, I am just going to use green for processes that are consuming less than 10, 000KB of memory, and red for processes that are consuming over 200,000,000 KB of memory. These values are completely arbitrary and do not actually mean anything. I just picked a range that would allow me to demonstrate the technique.

To make this happen, I am going to use a series of if-then-else statements. Here is what the script looks like:

$Processes = Get-Process
Write-Host ‘Process Name’`t `t ‘Process ID’ `t ‘Process Working Set’
ForEach ($Process in $Processes)
{
Write-Host -NoNewLine $Process.ProcessName
Write-Host -NoNewLine `t `t `t
Write-Host -NoNewLine $Process.ID
Write-Host -NoNewLine `t `t
If ($Process.WorkingSet -lt 10000){Write-Host $Process.WorkingSet -ForegroundColor White -BackgroundColor Green}
If ($Process.WorkingSet -GT 200000000){Write-Host $Process.WorkingSet -ForegroundColor White -BackgroundColor Red}
If ($Process.WorkingSet -GT 10000 -and $Process.WorkingSet -lt 200000000){Write-Host $Process.WorkingSet}
}

The first half of the script works in exactly the same way as what I showed you a moment ago. However, you will notice that I have added three “if” statements to the script.

The first “if” statement checks to see if the working set size is less than (-LT) 10,000KB. If so, then it writes the value using a green color. The second “if” statement checks to see if the value is greater than (-GT) 200,000,000KB. If so, then the value is displayed in red. The last “if” statement checks to see if the value is greater than 10,000 but less than 200,000,000. If so, then the value is displayed using the default text color. You can see the output shown in the figure below. Notice that one high value is displayed in red and one low value is displayed in green.

operating system health

Color your world

As you can see, PowerShell makes it relatively easy for you to color-code your script’s output. In this case, I have based the color-coding on a numerical range, but you could create a color code for just about anything. It is also worth noting that even though I have used a text-based interface for this particular example, PowerShell fully supports the creation of color-coded HTML reports.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top