Skip to content

Instantly share code, notes, and snippets.

@turibbio
Created August 18, 2015 23:08
Show Gist options
  • Save turibbio/f9e1cd808cfed6a7bcc1 to your computer and use it in GitHub Desktop.
Save turibbio/f9e1cd808cfed6a7bcc1 to your computer and use it in GitHub Desktop.
Write colored output with Powershell cmdlet Write-Output
function Write-FormattedOutput
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)][Object] $Object,
[Parameter(Mandatory=$False)][ConsoleColor] $BackgroundColor,
[Parameter(Mandatory=$False)][ConsoleColor] $ForegroundColor
)
# save the current color
$bc = $host.UI.RawUI.BackgroundColor
$fc = $host.UI.RawUI.ForegroundColor
# set the new color
if($BackgroundColor -ne $null)
{
$host.UI.RawUI.BackgroundColor = $BackgroundColor
}
if($ForegroundColor -ne $null)
{
$host.UI.RawUI.ForegroundColor = $ForegroundColor
}
Write-Output $Object
# restore the original color
$host.UI.RawUI.BackgroundColor = $bc
$host.UI.RawUI.ForegroundColor = $fc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment