Colorizing the output

Kasper Katzmann
Kasper Katzmann used Ask the Experts™
on
I have this script, that show all the functions within a custom module called SIT-ADFunctions. It works great, but I would really like to colorize the out so that each line shifts between lets say white and yellow. Is it possible?
	Get-Command -Module SIT-ADFunctions | ForEach-Object {
		$ReturnObject = '' | Select-Object -Property Function, Alias, Synopsis
		$ReturnObject.Function = $_.Name
		$ReturnObject.Alias = Get-Alias -Definition $_.Name -ErrorAction SilentlyContinue
		$ReturnObject.Synopsis = (Get-Help -Name $_.Name -ErrorAction SilentlyContinue).synopsis 
		$ReturnObject
	} | Format-Table -AutoSize

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Try this:
Function Format-AlternateColor {
[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$true)]
	[string[]]$Object,
	[ConsoleColor]$EvenColor = 'White',
	[ConsoleColor]$OddColor = 'Yellow',
	[ConsoleColor]$SkipColor = 'Gray',
	[Int32]$Skip = 0
)
	Begin {
		$lineCount = 0
	}
	Process {
		$Object -split "`r`n" | ForEach-Object {
			If ($lineCount -lt $Skip) {
				$color = $SkipColor
			} ElseIf ($lineCount % 2) {
				$color = $OddColor
			} Else {
				$color = $EvenColor
			}
			Write-Host -Object $_ -ForegroundColor $color
			$lineCount++
		}
	}
}
	Get-Command -Module SIT-ADFunctions | ForEach-Object {
		$ReturnObject = '' | Select-Object -Property Function, Alias, Synopsis
		$ReturnObject.Function = $_.Name
		$ReturnObject.Alias = Get-Alias -Definition $_.Name -ErrorAction SilentlyContinue
		$ReturnObject.Synopsis = (Get-Help -Name $_.Name -ErrorAction SilentlyContinue).synopsis 
		$ReturnObject
	} | Format-Table -AutoSize | Out-String | Format-AlternateColor -Skip 3

Open in new window

Kasper KatzmannSeniorkonsulent

Author

Commented:
Wauw - that was spot on. Thank you very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial