Write-ColoredOutput.psm1
Function Write-ColoredOutput { <# .Synopsis Writes output to pipeline yet capable to give colored output to the screen .DESCRIPTION Sometimes you want to apply some colors to your output at the sametime you want to send it over pipeline which is not possible using native PowerShell cmdlets like, Write-Host and Write-Output. This advanced function allows you to achive the same. After writing the output the foreground and background colors will be reset. .EXAMPLE $P = Get-Process PS C:\>Write-ColoredOutput $P PS C:\>$p Nothing surprising just behaves like Write-Output .EXAMPLE $P = Get-Process PS C:\>Write-ColoredOutput $P -ForegroundColor Green PS C:\>$p The output will be written to the screen with Green foreground color .EXAMPLE $P = Get-Process PS C:\>Write-ColoredOutput $P -BackgroundColor Green PS C:\>$p The output will be written to the screen with Green background color .EXAMPLE $P = Get-Process PS C:\>Write-ColoredOutput $P -ForegroundColor Green -BackgroundColor Cyan PS C:\>$p The output will be written to the screen with Green foreground color and Cyan background color .INPUTS PSObject .OUTPUTS The input type #> [CMDLetBinding()] Param( [Parameter(Mandatory,ValueFromPipeline,Position=0)][ValidateNotNull()][ValidateNotNullOrEmpty()] [PSObject[]]$InputObject ) DynamicParam { $ForegroundColor = 'ForegroundColor' try { $List = [System.ConsoleColor]::GetNames([System.ConsoleColor]) } catch{ throw } $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $false $ParameterAttribute.Position = 1 $AttributeCollection.Add($ParameterAttribute) $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $arrSet1 = $List $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet1) $AttributeCollection.Add($ValidateSetAttribute) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ForegroundColor, [System.ConsoleColor ], $AttributeCollection) $RuntimeParameterDictionary.Add($ForegroundColor, $RuntimeParameter) $BackgroundColor = 'BackgroundColor' $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute.Mandatory = $false $ParameterAttribute.Position = 2 $AttributeCollection.Add($ParameterAttribute) $arrSet2 = $List $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet2) $AttributeCollection.Add($ValidateSetAttribute) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($BackgroundColor, [System.ConsoleColor], $AttributeCollection) $RuntimeParameterDictionary.Add($BackgroundColor, $RuntimeParameter) return $RuntimeParameterDictionary } BEGIN { $inputs = $PSBoundParameters if($inputs["BackgroundColor"] -notmatch "^$") { [System.Console]::BackgroundColor = $inputs["BackgroundColor"] } if($inputs["ForegroundColor"] -notmatch "^$") { [System.Console]::ForegroundColor = $inputs["ForegroundColor"] } } PROCESS { "ForegroundColor","BackgroundColor" | %{ $null = $inputs.Remove($_) } Write-Output $InputObject } END { [Console]::ResetColor() } } |