Zachary Loeber

I eat complexity and am never without a meal.

Powershell: Colorize-Table Function

Here is a function I modified for quickly modifying the attributes of a table’s rows or individual cells. It uses Linq and is blistering fast. The results are pretty as well so that doesn’t hurt either.

Here is the script on the Technet Gallery

Here is an example which produces a semi-useful example report of the top memory consuming processes.  Anything taking over 150 Mb is highlighted yellow. Above 400 Mb gets highlighted red. The order for processing the table is obviously important, so they don’t all end up the same color. For this example we highlight the entire row but the individual cells can just as easily be color coded with this function (or styled or whatever fits your fancy actually).

By default the table is boring. This custom header is used to add some borders and such.

Now gather all processes, include the name, company, and Memory. Rename WS to Memory for readability, truncate, and convert to Mb. then sort in reverse order and return the top 5 results.

This scriptblock is passed directly into the function, the variable names are important and should not change as they are used in the function (because I wasn’t able to get abstraction to work properly for whatever reason). If you really like you can replace [int]$args1 with some static number or string and the ColumnValue parameter will be ignored when passed to the function. I also found it was important to use strong typesetting in the script block. If you don’t need anything fancy and just want to change a cell/row based on what value is in it then don’t pass any scriptblock and a general equal comparison will be performed on ColumnValue which you will have to pass instead.

Run the first passthrough for any columns with the Memory value above 150 to highlight the row yellow. This is the first time through so I tack on the HTMLHead.

Then run again for the same row for any items above 400 to highlight the row red.

Spit out the report to a file and open it up.

The resulting table