Format Table Colors in PowerShell

Did anyone think of formatting the output table as result of array of Objects/PSObjects on PowerShel

Did anyone think of formatting the output table as result of array of Objects/PSObjects on PowerShell host window with more fancy coloring?
I wonder if any imagine if we can change the fore color or the back color of the headers rather than the body (rows) colors?
What about formatting the table to have the odd rows with different fore/back colors rather than the even ones?
What about putting different separator line for the header the dashed one? Or, what about just remove it, or color it with different fore/back color?
What about putting separators between rows? And what if we can put those separators with different fore/back colors?
What about presenting the values for specific columns/properties with different fore/back colors?
What about some conditional formatting such as color specific values/cells when the values matches specific queries? Or what about color some values based on some complicated composite queries applies on multiple properties?

I had built my crazy PowerShell function “Write-PSObject” (pipeline supported) which can achieve all the imaginations and so much more with combination of over than 30 parameters which they can be used together or separately wisely to format your results with more fancy meaningful colors (fore/back).

Download the Function!
Download the Function with detailed examples!

The following table represents the parameters list of the function:

SN

Name

Alias

Data Type

Mandatory

Description

Default Value

Examples

Notes

Prerequisite Parameters 

01

Object

O, I

Object

True

The input Objects(s)/PSOject(s) to display with formatted colors.

N/A

N/A

N/A

N/A

02

MatchMethod MM, M String[] False The array of values validations way which must be provided with one of the three valid set "Exact", "Match" or "Query". N/A N/A If this parameter not provided, the conditional formatting will not be functional and all the tailed parameters will be ignored. This must be tailed with the following parameters to be functional: "Column", "Value" and one or more of the following parameters: "ValueForeColor", "ValueBackColor", "RowForeColor" and/or "RowBackColor". N/A

03

FormatTableColor

FTC, FT, F

Switch

False

If specified, lines will be formatted with specified fore/back colors based on the sequence.

User can define specific fore or/and back color for odd lines in sequence, and different fore or/and back colors for even lines.

N/A

N/A

User needs to use one or more of the following parameters with this switch in order to make it functional:

"OddLineForeColor", "OddLineBackColor", "EvenLineForeColor" and/or "EvenLineBackColor"

N/A

04

Column C, Col String[] False The list of the names of columns/properties which hold the value(s) to be validated to apply the table formatting. * N/A This parameter can provided as one string or array of them which must match the same count of the "Value" parameter or the script will be terminated (If the "$IgnoreErrors" switch is provided, the function will not be terminated, but the formatting will not be functional).   If the parameter "MatchMethod" is not provided, this column and all the other relative ones will be ignored and the whole conditional formatting will not be functional.   The "*" is referring to all the columns/properties within the table/PSObject(s). If the "*" is provided as a name of column/property, that means the same condition defined by the "MatchMethod" and "Value" parameters will be applied on all the columns/properties. For example, you can just mention "*" as column/property name to color all the "False" values for any column/property with  forecolor "Red" and/or backcolor "Black". MatchMethod

05

Value

V

String[]

False

The value validation way which must be matching with one of the following three valid set provided by the parameter "MatchMethod" (all case insensitive):

1. Exact ---» The provided value must match exactly with the inputs.

     Ex1: -Value "True"

     Ex2: -Value $True

     Ex3: -Value 4

     Ex4: -Value "Ahmad", 4, "True", $False

2. Match ---» The provided value must match with part of the inputs.

     Ex1: "Ahmad"

     Ex2: -Value "Ah"

     Ex3: -Value "ma"

     Ex4: -Value "ad"

     Ex5: -Value "X", "C123", "A"

3. Query ---» The provided value must match with the provided query which could contains various conditions on the same column.

     Ex1: -Value "'Up Time' -Like '00 Days*'"

     Ex2: -Value "'CPU' -gt 90 -and CPU -le 95'"

     Ex3: -Value "'Name' -Match 'C3' -and 'Name' -Notmatch 'C34'"

     Ex4: -Value "'Name' -Match 'C3' -and 'Name' -Notmatch 'C34'", "'Up Time' -Like '00 Days*'"

     Ex5: -Value "'Name' -Match 'Ahmad' -and 'Address' -Notmatch 'Central Park'", "'Up Time' -Like '00 Days*'"

 

Please note that, column/property name must be put as it is between two single quotations, and the whole condition/query between two double quotations.

N/A

N/A

This must be tailed with the following parameters to be functional:

"Column", "Value" and one or more of the following parameters:

"ValueForeColor", "ValueBackColor", "RowForeColor" and/or "RowBackColor".

 

This parameter can provided as one string value or array of them which must match the same count of the "Column" parameter or the script will be terminated (If the "$IgnoreErrors" switch is provided, the function will not be terminated, but the formatting will not be functional).

 

If the parameter "MatchMethod" is not provided, this column and all the other relative ones will be ignored and the whole conditional formatting will not be functional.

MatchMethod

Column

06

FormatTableColor FTC, FT, F Switch False If specified, lines will be formatted with specified fore/back colors based on the sequence.   User can define specific fore or/and back color for odd lines in sequence, and different fore or/and back colors for even lines. N/A N/A User needs to use one ore more of the following parameters with this switch in order to make it functional: "OddLineForeColor", "OddLineBackColor", "EvenLineForeColor" and/or "EvenLineBackColor" N/A

07

ValueForeColor

VFC

ConsoleColor[]

False

Used to define the forecolor of the values that match the specified condition.

N/A

Red, Black, Blue, White, etc.

This parameter is function only when the following parameters are provided "MatchMethod" “Column” and “Value ”.

MatchMethod

Column

Value

08

ValueBackColor VBC ConsoleColor[] False Used to define the back color of the values that match the specified condition. N/A Red, Black, Blue, White, etc. This parameter is function only when the following parameters are provided "MatchMethod" “Column” and “Value”. MatchMethod Column Value

09

RowForeColor

RFC

ConsoleColor

False

Used to define the forecolor of the whole line/row that when of its values match the specified condition(s).

 

With another meaning, if no values inside that row/line (single PSObject) matches any provided condition, this parameter will be ignored.

N/A

Red, Black, Blue, White, etc.

If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

 

The "FlagsForeColor " of the flagged columns would override it.

MatchMethod

Column

Value

10

RowBackColor RBC ConsoleColor False Used to define the back color of the whole line/row that any of its values match the specified condition(s).   With another meaning, if no values inside that row/line (single PSObject) matches any provided condition, this parameter will be ignored. N/A Red, Black, Blue, White, etc. This parameter is function only when the "MatchMethod" parameter is provided. If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).   The "FlagsBackColor" of the flagged columns would override it. MatchMethod Column Value

11

OddLineForeColor

OLFC

ConsoleColor

False

Used to define the forecolor of the whole line/row that with an odd sequence of the whole rows starting from the first row of values.

 

Example, you can provide that parameter with the forecolor of the rows positions 1, 3, 5, 7, etc.

The default forecolor of the host.

Red, Black, Blue, White, etc.

This parameter is function only when the "FormatTableColor" parameter is provided.

It can work with or without the other relative odd/even fore/back colors.

 

If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

If the "RowForeColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s).

The "FlagsForeColor " of the flagged columns would override it.

FormatTableColor

12

OddLineBackColor OLBC ConsoleColor False Used to define the back color of the whole line/row that with an odd sequence of the whole rows starting from the first row of values.   Example, you can provide that parameter with the back color of the rows number 1, 3, 5, 7, etc. The default back color of the host. Red, Black, Blue, White, etc. This parameter is function only when the "FormatTableColor" parameter is provided. It can work with or without the other relative odd/even fore/back colors.   If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s). If the "RowBackColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s). The "FlagsBackColor" of the flagged columns would override it. FormatTableColor

13

EvenLineForeColor

ELFC

ConsoleColor

False

Used to define the forecolor of the whole line/row that with an even sequence of the whole rows starting from the second row of values.

 

Example, you can provide that parameter with the forecolor of the rows number 2, 4, 6, 8, etc.

The default forecolor of the host.

Red, Black, Blue, White, etc.

This parameter is function only when the "FormatTableColor" parameter is provided.

It can work with or without the other relative odd/even fore/back colors.

 

If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

If the "RowForeColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s).

The "FlagsForeColor " of the flagged columns would override it.

FormatTableColor

14

EvenLineBackColor ELBC ConsoleColor False Used to define the back color of the whole line/row that with an odd sequence of the whole rows starting from the second row of values.   Example, you can provide that parameter with the back color of the rows number 2, 4, 6, 8, etc. The default back color of the host. Red, Black, Blue, White, etc. This parameter is function only when the "FormatTableColor" parameter is provided. It can work with or without the other relative odd/even fore/back colors.   If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s). If the "RowBackColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s). The "FlagsBackColor" of the flagged columns would override it. FormatTableColor

15

HeadersForeColor

HFC

ConsoleColor

False

Used to define the forecolor of the whole two headers lines/rows.

 

The first header row/line which describes the names of the columns/properties.

While, the second header row/line is the underlines dashes characters which separate the header names than the rows' values.

 

Example, When get the results of the function "Get-ChildItem -Path C:\ | FT -Al;", the output would be something like the following:

Mode             LastWriteTime Length Name   # First header line.

----             ------------- ------ ----   # Second header line.

The default forecolor of the host.

Red, Black, Blue, White, etc.

This parameter is not dependent on any other parameters or conditions.

N/A

16

HeadersBackColor HBC ConsoleColor False Used to define the back color of the whole two headers lines/rows.   The first header row/line which describes the names of the columns/properties. While, the second header row/line is the underlines dashes characters which separate the header names than the rows' values.   Example, When get the results of the function "Get-ChildItem -Path C:\ | FT -Al;", the output would be something like the following: Mode             LastWriteTime Length Name   # First header line. ----             ------------- ------ ----   # Second header line. The default back color of the host. Red, Black, Blue, White, etc. This parameter is not dependent on any other parameters or conditions. N/A

17

BodyForeColor

BFC

ConsoleColor

False

Used to define the forecolor of the whole lines/rows values.

The default forecolor of the host.

Red, Black, Blue, White, etc.

This parameter is not dependent on any other parameters or conditions.

 

If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

If the "RowForeColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s).

If the "OddLineForeColor" and /or "EvenLineForeColor" parameter(s) are provided, they would override it.

The "FlagsForeColor " of the flagged columns would override it.

N/A

18

BodyBackColor BBC ConsoleColor False Used to define the back color of the whole lines/rows values. The default back color of the host. Red, Black, Blue, White, etc. This parameter is not dependent on any other parameters or conditions.   If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s). If the "RowBackColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s). If the "OddLineBackColor" and /or "EvenLineBackColor" parameter(s) are provided, they would override it. The "FlagsBackColor" of the flagged columns would override it. N/A

19

ColoredColumns

CC

String[]

False

Define the columns/properties to have their values colored without conditions.

N/A

"CPU", "Memory", "SN"

This parameter is not dependent on any other parameters or conditions.

N/A

20

ColumnForeColor CFC ConsoleColor[] False Used to define the forecolor of values the specified "ColoredColumns". N/A Red, Black, Blue, White, etc. This parameter is function only when the "ColoredColumns" parameter is provided.   If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s). If the "RowForeColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s). If the "OddLineForeColor" and /or "EvenLineForeColor" parameter(s) are provided, they would override it. The "FlagsForeColor" of the flagged columns would override it.        ColoredColumns

21

ColumnBackColor

CBC

ConsoleColor[]

False

Used to define the back color of the whole lines/rows values.

N/A

Red, Black, Blue, White, etc.

This parameter is function only when the "ColoredColumns" parameter is provided.

 

If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

If the "RowBackColor" is provided, it could override the color of the whole line/row, if they match with specific provided condition(s).

If the "OddLineBackColor" and /or "EvenLineBackColor" parameter(s) are provided, they would override it.

The "FlagsBackColor" of the flagged columns would override it.        

ColoredColumns

22

FlagColumns FC String[] False Define the columns/properties to have their values colored when any of the specified values match the specified condition(s). N/A "CPU", "Memory", "SN" This parameter is function only when the following parameters are provided "MatchMethod" “Column” and “Value”. MatchMethod Column Value

23

FlagsForeColor

FFC

ConsoleColor[]

False

Used to define the forecolor of the flagged columns/properties.

N/A

Red, Black, Blue, White, etc

This parameter is function only when the ""MatchMethod" “Column”, “Value” and "FlagColumns" parameters are provided.

 

If the "ValueForeColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s).

This would override the colors specified by the "RowBackColor", "OddLineFBackColor" and/or the "EvenLineBackColor " parameters.

MatchMethod

Column

Value

FlagColumns

24

FlagsBackColor FBC ConsoleColor[] False Used to define the back color of the whole lines/rows values. The default back color of the host.  Red, Black, Blue, White, etc This parameter is function only when the ""MatchMethod" “Column”, “Value” and "FlagColumns" parameters are provided.   If the "ValueBackColor" is provided, it could override the colors of the matched properties values, if they match with specific provided condition(s). This would override the colors specified by the "RowForeColor", "OddLineForeColor" and/or the "EvenLineForeColor" parameters. MatchMethod Column Value FlagColumns

25

InjectRowsSeparator

IRS

Switch

False

If specified, new lines would be injected between body rows/lines.

N/A

N/A

By default the new line separator would be just new line with null data as the default value of the "RowsSeparator " parameter is $null.

N/A

26

RowsSeparator RS String False Define the shape of the separator line/row between body rows/lines.   The value could be one character such as "-", "==", etc., or mixed ones and the line would be trimmed by the maximum length of the body rows. $null "-", ".", "=", "|", "#", " ", etc. This parameter is function only when the "InjectRowsSeparator" parameter is provided.   If not specified, the new line separator would be just new line with null data. InjectRowsSeparator

27

RowsSeparatorForeColor

RSFC

ConsoleColor

False

Used to define the forecolor of the separator rows/lines.

The default forecolor of the host.

Red, Black, Blue, White, etc.

his parameter is function only when the "InjectRowsSeparator" and "RowsSeparator " parameters are provided.

InjectRowsSeparator

RowsSeparator

28

RowsSeparatorBackColor RSBC ConsoleColor False Used to define the back color of the separator rows/lines. The default back color of the host. Red, Black, Blue, White, etc. This parameter is function only when the "InjectRowsSeparator" and "RowsSeparator" parameters are provided. InjectRowsSeparator RowsSeparator

29

RemoveHeadersSeparator

RHS

Switch

False

Neglect displaying the second header line "----" (the separator line between headers (columns/properties) names and the body rows/values.).

 

With another meaning, the values rows/lines would be printed directly after the columns/properties names line/row.

N/A

N/A

This parameter will not be functional if "BodyOnly " parameter is specified.

N/A

30

HeadersSeparator HS String False Define the shape of the separator header separator line "----" between the columns names and body rows.   The value could be one character such as ".", "==", etc., or mixed ones and the line would be trimmed by the maximum length of the body rows. "-" ".", "=", "|", "#", " ", etc. This parameter will not be functional if any of the two parameters "BodyOnly" or "RemoveHeadersSeparator" is specified. N/A

31

HeadersSeparatorForeColor

HSFC

ConsoleColor

False

Used to define the forecolor of the separator header separator line "----" between the columns names and body rows.

The default forecolor of the host.

Red, Black, Blue, White, etc.

This parameter will not be functional if any of the two parameters "BodyOnly" or "RemoveHeadersSeparator " is specified.

N/A

32

HeadersSeparatorBackColor HSBC ConsoleColor False Used to define the back color of the separator header separator line "----" between the columns names and body rows.   Red, Black, Blue, White, etc. This parameter will not be functional if any of the two parameters "BodyOnly" or "RemoveHeadersSeparator" is specified. N/A

33

BodyOnly

BO

Switch

False

If specified, only the body rows (values lines) will be displayed, and, the two headers lines will not be displayed.

N/A

N/A

This parameter is not dependent on any other parameters or conditions.

N/A

34

HeadersOnly HO Switch False If specified, only the two headers lines will be displayed, and, the body rows (values lines) will not be displayed. N/A N/A This parameter is not dependent on any other parameters or conditions. N/A

35

IgnoreErrors

IE

Switch

False

It would try to suppress and error/exception could be raised due to missing or non-matched parameters and continue displaying the rows.

N/A

N/A

This parameter is not dependent on any other parameters or conditions.

 

If one of the provided conditions which is provided by the combination of the properties "MatchMethod",  "Column", "Value" and/or "FlagColumns" and/or their relative properties was not well provided, or mismatching, the "MatchMethod " property would be ignored and the conditional formatting will not be functional.

N/A

36

HostWindowWidth HWW UInt16 False Resize the host PowerShell window with a new specified width before presenting the data. N/A N/A This parameter is not dependent on any other parameters or conditions.   It would also resize the buffer width size with the same specified value if the current value is less than the new specified window width. N/A

37

HostWindowHeight

HWH

UInt16

False

Resize the host PowerShell window with a new specified height before presenting the data.

N/A

N/A

This parameter is not dependent on any other parameters or conditions.

 

It would also resize the buffer height size with the same specified height if the current value is less than the new specified window height.

N/A

38

HostWindowForeColor HWFC ConsoleColor False Override the current forecolor of the host PowerShell with a new specified one before presenting the data. N/A N/A This parameter is not dependent on any other parameters or conditions. N/A

39

HostWindowBackColor

HWBC

ConsoleColor

False

Override the current background color of the host PowerShell with a new specified one before presenting

N/A

N/A

This parameter is not dependent on any other parameters or conditions.

N/A

  

Examples:

I had created a sample data in XML text format which you need to run it first to apply all the following examples on it, or you can download the full script with all the attached examples from here.

XML Data Sample

#region XML Data Sample
$xml = [XML] "<Servers><Server SN='01' Server='SPWFE01' IP='192.168.0.10' Manufacture='HP' MemoryMB='32768' FreeMemoryMB= '10240' CPUCores='8' HyperThreading='False' Virtualization='Disabled' HyperVSupport='True' /><Server SN='02' Server='SPWFE02' IP='192.168.1.3' Manufacture='Dell' MemoryMB='32768' FreeMemoryMB= '30720' CPUCores='8' HyperThreading='True' Virtualization='Disabled' HyperVSupport='True' /><Server SN='03' Server='SPWFE03' IP='192.168.0.22' Manufacture='HP' MemoryMB='32768' FreeMemoryMB= '510' CPUCores='8' HyperThreading='True' Virtualization='Disabled' HyperVSupport='False' /><Server SN='04' Server='SQLPR01' IP='192.168.1.5' Manufacture='HP' MemoryMB='65536' FreeMemoryMB= '5120' CPUCores='16' HyperThreading='True' Virtualization='Enabled' HyperVSupport='True' /><Server SN='05' Server='SQLMI01' IP='192.168.1.6' Manufacture='Dell' MemoryMB='65536' FreeMemoryMB= '6420' CPUCores='16' HyperThreading='False' Virtualization='Enabled' HyperVSupport='True' /></Servers>";
$servers = [PSObject[]] $xml.Servers.Server  | Select SN, Server, IP, Manufacture, MemoryMB, FreeMemoryMB, CPUCores, HyperThreading, Virtualization, HyperVSupport;
#endregion XML Data Sample

The examples would be divided into two categories; the general formatting and the conditional formatting.

A. General Formatting

(Does not rely on specific conditions)

Example 01

Display the input object as it is without any formatting (Would act exactly as Write-Output).

Write-PSObject -object $servers;
# ----- Or -----
Write-PSObject $servers;
# ----- Or -----
$servers | Write-PSObject; #Pipeline supported.

 

Example 02

Display the body rows/lines (values) only.

Write-PSObject -Object $servers -BodyOnly;
# ----- Or -----
Write-PSObject $servers -BodyOnly;
# ----- Or -----
$servers | Write-PSObject -BodyOnly; #Pipeline supported.

Example 03

Display the Headers only.

Write-PSObject $servers -HeadersOnly;

 

Example 04

Display the Headers only and remove the header separator line (display only row/line which displays the columns/properties name).

Write-PSObject $servers -HeadersOnly -RemoveHeadersSeparator;

Example 05

Display the Headers with White forecolor, and, Blue background color. And the body rows/lines (values) with Yellow forecolor, and, DarkRed background color.

Write-PSObject $servers -BodyForeColor Yellow -BodyBackColor DarkRed -HeadersForeColor White -HeadersBackColor Blue;

Example 06

Display the input object as formatted table which displays each row/line in odd sequence (starting with the first body row) with "Cyan" forecolor.
Also, display each row/line in even sequence (starting with the second body row) with "Yellow" Fore Color.

Write-PSObject $servers -FormatTableColor -OddRowForeColor Cyan -EvenRowForeColor Yellow;

Example 07

Display the input object as formatted table which displays each row/line in odd sequence (starting with the first body row) with "DarkRed" background color.
Also, display each row/line in even sequence (starting with the second body row) with "Blue" background color.

Write-PSObject $servers -FormatTableColor -OddRowBackColor DarkRed -EvenRowBackColor Blue;

Example 08

Display the input object as formatted table which displays each row/line in odd sequence (starting with the first body row) with "Black" forecolor and White background color..
Also, display each row/line in even sequence (starting with the second body row) with "Yellow" Fore Color and Blue background color.

Write-PSObject $servers -FormatTableColor -OddRowForeColor Black -OddRowBackColor White -EvenRowForeColor Yellow -EvenRowBackColor Blue;

Example 09

Display the table with new linefeed between values/body rows/lines.

Write-PSObject $servers -InjectRowsSeparator;

Example 10

Display the table with new line of underscore characters ("_") between the values/body rows/lines.

Write-PSObject $servers -InjectRowsSeparator -RowsSeparator "_";

Example 11

Display the table with new dotted line between the values/body rows/lines with forecolor "Cyan".

Write-PSObject $servers -InjectRowsSeparator -RowsSeparator "." -RowsSeparatorForeColor Cyan;

Example 12

Display the table with new line of white spaces (" ") between the values/body rows/lines with background color "White".

Write-PSObject $servers -InjectRowsSeparator -RowsSeparator " " -RowsSeparatorBackColor White;

Example 13

Display the table with new line of combination of characters ("=*=.-^*") between the values/body rows/lines with forecolor "Black" and background color "White".

Write-PSObject $servers -InjectRowsSeparator -RowsSeparator "=*=.-^*" -RowsSeparatorForeColor Black -RowsSeparatorBackColor White;

Example 14

Replace the headers separator line/row (dashed line) with the equal character ("=") instead of the hyphen character ("-") with fore color Black, background color "Yellow".

Write-PSObject $servers -HeadersSeparator "=" -HeadersSeparatorForeColor Black -HeadersSeparatorBackColor Yellow;

Example 15

Display the table after forcing resizing the host PowerShell Window and changing the whole host forecolor (Green) and background color (Black).

Write-PSObject $servers -HostWindowWidth 150 -HostWindowHeight 50 -HostWindowForeColor Green -HostWindowBackColor Black;

Example 16

Display the values of the column/property "SN" with "Blue" forecolor and the values column/property "CPUCores" with "Yellow" forecolor.

Write-PSObject $servers -HostWindowWidth 150 -HostWindowHeight 50 -HostWindowForeColor Green -HostWindowBackColor Black;

 

 

 

B. Conditional Formatting

(Relies on specific condition or more)

Example 01

# Column: *
# Condition: Any property with value equals to $False
# Query: N/A
# Value Fore Color: Red
# Value Back Color: N/A
# Row Fore Color: N/A
# Row Back Color: N/A
# ----------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact -Column * -Value $false -ValueForeColor Red;

Example 02

# Column: Server
# Condition: Server matches "WFE"
# Query: 'Server' -Match 'WFE'
# Value Fore Color: White
# Value Back Color: Blue
# Row Fore Color: N/A
# Row Back Color: N/A
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Match -Column Server -Value WFE -ValueForeColor White -ValueBackColor Blue;

Example 03

# Column: Manufacture
# Condition: Manufacture "HP"
# Query: 'Manufacture' -EQ 'HP'
# Value Fore Color: N/A
# Value Back Color: N/A
# Row Fore Color: White
# Row Back Color: DarkCyan
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact -Column "Manufacture" -Value "HP" -RowForeColor White -RowBackColor DarkCyan;

Example 04

# Column: Manufacture
# Condition: Manufacture "HP"
# Query: 'Manufacture' -EQ 'HP'
# Value Fore Color: Yellow
# Value Back Color: Red
# Row Fore Color: White
# Row Back Color: Blue
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact -Column "Manufacture" -Value "HP" -ValueForeColor Yellow -ValueBackColor Red -RowForeColor White -RowBackColor Blue;

Example 05

# Column 01: Manufacture
# Condition 01: Manufacture "HP"
# Query 01: 'Manufacture' -EQ 'HP'
# Value Fore Color 01: Yellow
# Value Back Color 01: Red

# Column 02: Server
# Condition 02: Server matches "WFE"
# Query 02: 'Server' -Match 'WFE'
# Value Fore Color 02: White
# Value Back Color 02: Blue
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact, Match -Column "Manufacture", "Server"  -Value "HP", "WFE" -ValueForeColor Yellow, White -ValueBackColor Red, Blue;

Example 06

# Column 01: Manufacture
# Condition 01: Manufacture "HP"
# Query 01: 'Manufacture' -EQ 'HP'
# Value Fore Color 01: Yellow
# Value Back Color 01: Red

# Column 02: Server
# Condition 02: Server matches "WFE"
# Query 02: 'Server' -Match 'WFE'
# Value Fore Color 02: White
# Value Back Color 02: Blue

# Column 03: FreeMemoryMB
# Condition 03: Memory Usage Between 90% and 95%
# Query 03: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90
# Value Fore Color 03: Yellow
# Value Back Color 04: N/A
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact, Match, Query -Column "Manufacture", "Server", "FreeMemoryMB"  -Value "HP", "WFE", "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90" -ValueForeColor Yellow, White, Yellow -ValueBackColor Red, Blue;

Example 07

# Column 01: Manufacture
# Condition 01: Manufacture "HP"
# Query 01: 'Manufacture' -EQ 'HP'
# Value Fore Color 01: Yellow
# Value Back Color 01: Red

# Column 02: Server
# Condition 02: Server matches "WFE"
# Query 02: 'Server' -Match 'WFE'
# Value Fore Color 02: White
# Value Back Color 02: Blue

# Column 03: FreeMemoryMB
# Condition 03: Memory Usage Between 90% and 95%
# Query 03: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90
# Value Fore Color 03: Yellow
# Value Back Color 03: N/A

# Column 04: FreeMemoryMB
# Condition 04: Memory Usage greeter than 95%
# Query 04: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95
# Value Fore Color 04: Red
# Value Back Color 04: N/A
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact, Match, Query, Query -Column "Manufacture", "Server", "FreeMemoryMB", "FreeMemoryMB"  -Value "HP", "WFE", "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90", "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95" -ValueForeColor Yellow, White, Yellow, Red -ValueBackColor Red, Blue;

Example 08

# Column: *
# Condition: Any property with value equals to $False
# Query: N/A
# Value Fore Color: Red
# Value Back Color: N/A
# Row Fore Color: N/A
# Row Back Color: N/A

# Flag Columns: 'SN', 'Server'
# Flag Columns Fore Color: Cyan
# Flag Columns Back Color: N/A
# ----------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Exact -Column * -Value $false -ValueForeColor Red -FlagColumns "'SN', 'Server'" -FlagsForeColor Red;

Example 09

# Column 01: FreeMemoryMB
# Condition 01: Memory Usage Between 90% and 95%
# Query 01: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90
# Value Fore Color 01: Yellow
# Value Back Color 01: N/A
# Flag Columns 01: 'SN', 'Server', 'MemoryMB'
# Flag Columns Fore Color 01: Yellow
# Flag Columns Back Color 01: N/A

# Column 02: FreeMemoryMB
# Condition 02: Memory Usage greeter than 95%
# Query 02: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95
# Value Fore Color 02: Red
# Value Back Color 02: N/A

# Flag Columns 01: 'SN', 'Server', 'MemoryMB'
# Flag Columns Fore Color 01: Red
# Flag Columns Back Color 01: N/A
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Query, Query -Column "FreeMemoryMB", "FreeMemoryMB"  -Value "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90", "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95" -ValueForeColor Yellow, Red -FlagColumns "'SN', 'Server', 'MemoryMB'", "'SN', 'Server', 'MemoryMB'" -FlagsForeColor Yellow, Red;

Example 10

# Column 01: FreeMemoryMB
# Condition 01: Memory Usage Between 90% and 95%
# Query 01: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90
# Value Fore Color 01: Yellow
# Value Back Color 01: N/A
# Flag Columns 01: 'SN', 'Server', 'MemoryMB'
# Flag Columns Fore Color 01: Yellow
# Flag Columns Back Color 01: N/A

# Column 02: FreeMemoryMB
# Condition 02: Memory Usage greeter than 95%
# Query 02: (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95
# Value Fore Color 02: Red
# Value Back Color 02: N/A

# Flag Columns 02: 'SN', 'Server', 'MemoryMB'
# Flag Columns Fore Color 02: Red
# Flag Columns Back Color 02: N/A

# Column 03: Virtualization
# Condition 03: N/A
# Query 03: N/A
# Value Fore Color 03: Green
# Value Back Color 03: N/A

# Column 04: HyperVSupport
# Condition 04: N/A
# Query 04: N/A
# Value Fore Color 04: Magenta
# Value Back Color 04: N/A
# ------------------------------------------------------------------------------------------------------
Write-PSObject $servers -MatchMethod Query, Query -Column "FreeMemoryMB", "FreeMemoryMB"  -Value "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -LT 95 -And (('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -GT 90", "(('MemoryMB' - 'FreeMemoryMB') / 'MemoryMB') * 100 -gt 95" -ValueForeColor Yellow, Red -FlagColumns "'SN', 'Server', 'MemoryMB'", "'SN', 'Server', 'MemoryMB'" -FlagsForeColor Yellow, Red -ColoredColumns Virtualization, HyperVSupport -ColumnForeColor Green, Magenta;


Those samples were just few of what the function can do!
You can mix many samples together by mixing the parameters to get more fancy colorful results.
If those samples are not enough to help you to build your own color formatting, please check the parameters table above, or feel free to leave a message/comment for me, and I will reply as soon as possible.

 

Updates:

Date: February, 26th, 2017
Update: Backward compatibility with PS version 2.0

Date: March, 04th, 2017
Update: Fixed a bug for Back Color overlapping.

Date: April, 03rd, 2017
Update: Fixed a regression bug for flag columns colors.

Date: September, 17th, 2017
Update: Fixed the error occurs when trying to use the default foreground color and the default background color in the ISE.

Date: October, 10th, 2017
Update: Fixed a logical bug when running a query against numbers.

Date: October, 22nd, 2017
Update: Fixed a logical bug which could occur with results of two columns only.

Comments (15) -

  • Getting this error on PSVersion 4.0...Write-Host : Cannot process the color because -1 is not a valid color.
    • Hi, usually we get this error when you try to run your code on the ISE. Nevertheless, this should have been handled already which could mean a bug in the script. Could you please support me with the line of code you used to check this? Also, could you confirm if you are using the ISE not the console?

      Thanks a lot.

      Best Regards,
      Ahmad
  • Ahmad,

    When I run this in the ISE the default background color is set to DarkMagenta.

    Is there any way to have it default to the ISE's standard console background color #FF012456?


    • Hi Winston,

      Thank you very much for your comment.

      The ISE doesn't have a default host environment foreground color or default background color, hence, I'm putting the default values of the PS Console instead.
      If you tried to run the following commands in ISE, you will see that the default values are "-1":
      (Get-Host).UI.RawUI.ForegroundColor
      (Get-Host).UI.RawUI.BackgroundColor

      To overcome this solution with the current version of my script, you can set the default colors on the ISE from "Tools>Options>Colors and Fonts".
      Or, you can download the updated version from the following link and give it a try.
      I would ask you a favor to give it a proper test; and then, send me your feedback before I upload it to my blog and to the MSDN repository:
      ahmad.jempress.com/.../...-PSObject-2017-09-17.ps1

      Looking forward to hearing back from you.

      Thank you very much in advance.

      Best Regards,
      Ahmad
  • Posted over on the gallery but thought i would post here as well.

    I am testing out your function and do think it is great. However i have ran into an issue. Its reproducible even using your $servers example. Please advise if i am doing something incorrect.

    Write-PSObject $servers -MatchMethod Query -Column "CPUcores" -Value "'CPUCores' -GT 2" -ValueForeColor Red

    With this example the 8's are highlighted but the 16's are not. After some testing its only comparing the 2 vs the first number of the CPUCores. Since the 8 is greater then the 2 the does highlight however from the 16 only the 1 is being used so it does not get highlighted.
    If the CPUCores would be something like 40 it would start to be highlighted again.

    I hope my example makes sense.
    • Hi,

      Thank you very much for your comment.
      I will check and update the thread soon.

      Thank you very much in advance for your patience.

      Best Regards,
      Ahmad
    • Hi,

      It was a bug when when you run your query against column(s) from numeric data type.
      The bug has been resolved and the updated script has been uploaded.

      Kindly download the updated script and give it a try, then send me your feedback.

      Thank you very much in advance.

      Best Regards,
      Ahmad
      • In my limited testing all seems good Smile

        Thanks for the quick fix and THANK YOU VERY MUCH for the sweet tool!
  • Another issue i do not understand.
    I feel its reproducible using this code.
    The first one the Id field looks right. In the second example the Id field is missing  a character and is being evaluated incorrectly.

    Write-PSObject (Get-Process | Select-Object name, id)
    Write-PSObject (Get-Process | Select-Object name, id) -MatchMethod query -Column Id -Value "'id' -GT 99" -ValueForeColor Red
  • I think i have ran into another issue.
    A way to repeat this is with the following commands.
    Write-PSObject (Get-Process | Select-Object name, id)
    Write-PSObject (Get-Process | Select-Object name, id) -MatchMethod query -Column Id -Value "'id' -GT 99" -ValueForeColor Red

    You will notice the ID column of the first command works correctly. The second command has an odd space in the command and incorrect highlighting.

    Somehow my first post on this disappeared.
    Thanks for the great tool. Hope we can squash these bugs.
    • Hi,

      Thank you very much for your comments again.
      First, let me just explain to you that your first or even second comment won't appear unless I manually approve them, which I did few minutes ago, so be patient please.

      I will check the new issue when I get free time as soon as possible.

      Thank you very much for your patience.

      Best Regards,
      Ahmad
      • After some playing around i noticed that the issue only happens if the property being displayed is like 3 characters or less.

        This function will be in my bag of goodies from now on.
        Thank you.

        JustusIV
        • Hi JustusIV,

          Thank you very much for your comment.
          I had identified the bug and it happens with very rare situation that's why I didn't see it before although I had used the function in different scripts.
          The bug requires revising to big parts of the code which would take time cause I need to work on it when I've free time.
          Please bear with me until I resolve the bug and thanks for you for identifying it.

          Best Regards,
          Ahmad
        • Hi JustusIV,

          I believe I had fixed the bug.
          Please download the updated script and send me your precious feedback.

          Thank you very much in advance.

          Best Regards,
          Ahmad
          • My limited testing has fixed my issue...
            Thanks so much for this!

            Such a great tool.

Add comment

Loading