Created
August 18, 2015 23:08
-
-
Save turibbio/f9e1cd808cfed6a7bcc1 to your computer and use it in GitHub Desktop.
Write colored output with Powershell cmdlet Write-Output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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