PowerShell function: Test-ConsoleColor provides a visual demonstration of the foreach scripting construct
Test-ConsoleColor
is a PowerShell function that I recently wrote to provide a visual demonstration
of how to loop through a series of objects in PowerShell using the
foreach scripting construct, not to be
confused with the ForEach-Object
cmdlet.
1#Requires -Version 3.0 -Modules Pscx
2function Test-ConsoleColor {
3
4<#
5.SYNOPSIS
6 Tests all the different color combinations for the PowerShell console.
7
8.DESCRIPTION
9 Test-ConsoleColor is a PowerShell function that by default iterates through
10 all of the possible color combinations for the PowerShell console. The PowerShell
11 Community Extensions Module is required by the function.
12
13.PARAMETER Color
14 One or more colors that is part of the System.ConsoleColor enumeration. Run
15 [Enum]::GetValues([System.ConsoleColor]) in PowerShell to see the possible values.
16
17.PARAMETER Paragraphs
18 The number of latin paragraphs to generate during each foreground color test.
19
20.PARAMETER Milliseconds
21 Specifies how long to wait between each iteration of color changes in milliseconds.
22
23.EXAMPLE
24 Test-ConsoleColor
25
26.EXAMPLE
27 Test-ConsoleColor -Color Red, Blue, Green
28
29.EXAMPLE
30 Test-ConsoleColor -Paragraphs 7
31
32.EXAMPLE
33 Test-ConsoleColor -Milliseconds 300
34
35.EXAMPLE
36 Test-ConsoleColor -Color Red, Green, Blue -Paragraphs 7 -Milliseconds 300
37
38.INPUTS
39 None
40
41.OUTPUTS
42 None
43
44.NOTES
45 Author: Mike F Robbins
46 Website: http://mikefrobbins.com
47 Twitter: @mikefrobbins
48#>
49
50 [CmdletBinding()]
51 param (
52 [ValidateNotNullOrEmpty()]
53 [System.ConsoleColor[]]$Color = [System.Enum]::GetValues([System.ConsoleColor]),
54
55 [ValidateNotNullOrEmpty()]
56 [int]$Paragraphs = 5,
57
58 [ValidateNotNullOrEmpty()]
59 [int]$Milliseconds = 100
60 )
61
62 if ($Host.Name -ne 'ConsoleHost') {
63 Throw 'This function can only be run in the PowerShell Console.'
64 }
65
66 $BG = [System.Console]::BackgroundColor
67 $FG = [System.Console]::ForegroundColor
68 $Title = [System.Console]::Title
69
70 foreach ($BGColor in $Color) {
71 [System.Console]::BackgroundColor = $BGColor
72 Clear-Host
73
74 foreach ($FGColor in $Color) {
75 [System.Console]::ForegroundColor = $FGColor
76 [System.Console]::Title = "ForegroundColor: $FGColor / BackgroundColor: $BGColor"
77 Clear-Host
78
79 Write-Verbose -Message "Foreground Color is: $FGColor"
80 Write-Verbose -Message "Background Color is $BGColor"
81
82 Get-LoremIpsum -Length $Paragraphs
83 Start-Sleep -Milliseconds $Milliseconds
84 }
85 }
86
87 [System.Console]::BackgroundColor = $BG
88 [System.Console]::ForegroundColor = $FG
89 [System.Console]::Title = $Title
90 Clear-Host
91
92}
The Test-ConsoleColor
function shown in this blog article can be downloaded from
my PowerShell repository on GitHub.
µ