Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Last active October 8, 2021 01:15
Show Gist options
  • Save Kieranties/90ff3b32f4645577a1f201f3092300bd to your computer and use it in GitHub Desktop.
Save Kieranties/90ff3b32f4645577a1f201f3092300bd to your computer and use it in GitHub Desktop.
Write information messages in PowerShell that support colors (like Write-Host) but always honors the context `$InformationPreference variable - https://blog.kieranties.com/2018/03/26/write-information-with-colours
<#
.SYNOPSIS
A simple example of Write-Host
#>
# Displays the message
Write-Host -Object 'Hello'
# Try to silence directly
Write-Host "Still shown :-(" -InformationAction SilentlyContinue
# Try to silence with global variable
$global:InformationPreference = 'SilentlyContinue'
Write-Host "Ignores global :-(" -InformationAction SilentlyContinue
#Requires -Version 5.0
using namespace System.Management.Automation
<#
.SYNOPSIS
Writes messages to the information stream, optionally with
color when written to the host.
.DESCRIPTION
An alternative to Write-Host which will write to the information stream
and the host (optionally in colors specified) but will honor the
$InformationPreference of the calling context.
In PowerShell 5.0+ Write-Host calls through to Write-Information but
will _always_ treats $InformationPreference as 'Continue', so the caller
cannot use other options to the preference variable as intended.
#>
Function Write-InformationColored {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Object]$MessageData,
[ConsoleColor]$ForegroundColor = $Host.UI.RawUI.ForegroundColor, # Make sure we use the current colours by default
[ConsoleColor]$BackgroundColor = $Host.UI.RawUI.BackgroundColor,
[Switch]$NoNewline
)
$msg = [HostInformationMessage]@{
Message = $MessageData
ForegroundColor = $ForegroundColor
BackgroundColor = $BackgroundColor
NoNewline = $NoNewline.IsPresent
}
Write-Information $msg
}
<#
.SYNOPSIS
Examples of using Write-InformationColored.
#>
# By default this would not appear as $InformationPreference = 'SilentlyContinue'
Write-InformationColored -MessageData 'Hello' -ForegroundColor Green
# This would output the message to the console
Write-InformationColored -MessageData 'Hello again' -InformationAction 'Continue' -ForegroundColor Yellow
# By setting $InformationPreference = 'Continue' we ensure any information message
# is displayed for any call
$InformationPreference = 'Continue'
Write-InformationColored -MessageData 'Still here' -Foregroundcolor Blue
# This would redirect the information stream to a file
Write-InformationColored -MessageData 'Talking to a file' -BackgroundColor Red 6> out.txt
<#
.SYNOPSIS
A simple example of Write-Information
#>
# By default this would not appear as $InformationPreference = 'SilentlyContinue'
Write-Information -MessageData 'Hello'
# This would output the message to the console
Write-Information -MessageData 'Hello again' -InformationAction 'Continue'
# By setting $InformationPreference = 'Continue' we ensure any information message
# is displayed for any call
$InformationPreference = 'Continue'
Write-Information -MessageData 'Still here'
# This would redirect the information stream to a file
Write-Information -MessageData 'Talking to a file' 6> out.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment