Skip to content

Out ConditionalColor

apobekiaris edited this page Sep 4, 2020 · 3 revisions

external help file: XpandPwsh-help.xml Module Name: XpandPwsh schema: 2.0.0

Out-ConditionalColor

SYNOPSIS

Display colorized pipelined output.

SYNTAX

Out-ConditionalColor [-Conditions] <PSObject> [[-Property] <String>] [-Inputobject] <PSObject[]>
 [<CommonParameters>]

DESCRIPTION

This command is designed to take pipeline input and display it in a colorized format, based on a set of conditions. Unlike Write-Host which doesn't write to the pipeline, this command will write to the pipeline. You can get colorized data and save the output to a variable at the same time, although you'll need to use the common OutVariable parameter (see examples).

The default behavior is to use a hash table with a property name and color. The color must be one of the standard console colors used with Write-Host.

$c = @{Stopped='Red';Running='Green'}

You can then pipe an expression to this command, specifying a property name and the hash table. If the property matches the key name, the output for that object will be colored using the corresponding hash table value.

get-service -diplayname windows* | out-conditionalcolor $c status

Or you can do more complex processing with an ordered hash table constructed using this format:

[ordered]@{ <comparison scriptblock> = <color>}

The comparison scriptblock can use $PSitem.

$h=[ordered]@{ {$psitem.ws -gt 200mb}='yellow' {$psitem.vm -gt 500mb}='red' {$psitem.cpu -gt 300}='cyan' }

When doing a complex comparison you must use an [ordered] hashtable as each key will be processed in order using an If/ElseIf statement.

This command should be the last part of any pipelined expression. If you pipe to anything else, such as Sort-Object, you will lose your color formatting. Do any other sorting or filtering before piping to this command.

This command requires PowerShell 3.0 and works best in the PowerShell console.

EXAMPLES

EXAMPLE 1

get-service -displayname windows* | out-conditionalcolor -conditions @{Stopped='Red'} -property Status

Get all services where the displayname starts with windows and display stopped services in red.

EXAMPLE 2

get-service -displayname windows* | out-conditionalcolor @{Stopped='Red'} status -ov winstop

Repeat the previous example, but save the output to the variable winstop. When you look at $Winstop you'll see the services, but they won't be colored. This example uses the parameters positionally.

EXAMPLE 3

get-eventlog system -newest 50 | out-conditionalcolor @{error='red';warning='yellow'}
Enter a property name: entrytype

Get the newest 50 entries from the System event log. Display errors in red and warnings in yellow. If you don't specify a property you will be prompted.

EXAMPLE 4

$c =[ordered]@{{$psitem.length -ge 1mb}='red';{$psitem.length -ge 500KB}='yellow';{$psitem.length -ge 100KB}='cyan'}
PS C:\> dir c:\scripts\*.doc,c:\scripts\*.pdf,c:\scripts\*.xml | Select Name,LastWriteTime,Length | out-conditionalcolor $c

The first command creates an ordered hashtable based on the Length property. The second command uses it to et certain file types in the scripts folder and display the selected properties in color depending on the file size.

PARAMETERS

-Conditions

Use a simple hashtable for basic processing or an ordered hash table for complex.

Type: PSObject
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Property

When using a simple hash table, specify the property to compare using the -eq operator. If you don't specify a property you will be prompted.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Inputobject

{{ Fill Inputobject Description }}

Type: PSObject[]
Parameter Sets: (All)
Aliases:

Required: True
Position: 3
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES

Last Updated: 9/17/2013 Version : 0.9

Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/


  • DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  • THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
  • YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  • DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.

RELATED LINKS

http://jdhitsolutions.com/blog/powershell/3462/friday-fun-out-conditionalcolor/

About_Hash_Tables

image

image


Star the project if you think it deserves it.

GitHub stars

Fork the project to extend and contribute.

GitHub forks

Clone this wiki locally