Skip to content
ConsoleMonster
Cmdlet

Write-ConsoleText

Module
ConsoleMonster
Inputs
System.Object[]

Writes coloured text with optional markup, alignment, wrapping and a text log.

Syntax

Write-ConsoleText
[-Alignment <Left|Center|Right>]
[-BackgroundColor <Object[]>]
[-Color <Object[]>]
[-Decoration <String[]>]
[-Gradient <Object[]>]
[-Indent <Int32>]
[-LinesAfter <Int32>]
[-LinesBefore <Int32>]
[-Link <String>]
[-LogFile <String>]
[-Markup <String>]
[-NoNewLine]
[-Role <ThemeToken>]
[-ShowTimestamp]
[-SplitLines <String>]
[[-Text] <Object[]>]
[-TimestampFormat <String>]
[-Width <Int32>]
[-Wrap]
[<CommonParameters>]
#
Parameter set: All Parameter Sets

Description

Text is literal; Color and BackgroundColor apply per piece. Role, Decoration, Link and Gradient apply to the message. Indent adds spaces and Alignment places text within Width. ShowTimestamp adds a timestamp. NoNewLine continues the current line. LogFile appends plain text with timestamps; new logs use UTF-8 and existing logs retain their encoding. Redirected output is plain text.

Examples

Example 1

$textOptions = @{
    Text = @('Checking group: ', 'Operators')
    Color = @('Gray', 'Green')
    Indent = 4
}
Write-ConsoleText @textOptions

Colour two pieces and indent the message

Example 2

$textOptions = @{
    Text = @('Maintenance complete')
    Role = 'Pass'
    Alignment = 'Center'
    Width = 60
}
Write-ConsoleText @textOptions

Centre a heading within sixty columns

Example 3

$textOptions = @{
    ShowTimestamp = $true
    Text = @('[info] ', "Path exists`n2. line`n3. line")
    Color = @('Yellow', 'White')
    SplitLines = "`n"
}
Write-ConsoleText @textOptions

A multi-line message aligned under the time stamp

Example 4

Get-ConsoleDemo -Slug 'prompt-tour' | Select-Object -ExpandProperty Source

Read the gallery script to see Write-ConsoleText in a complete example. Each lookup returns the script's source. Use the same slug with Show-ConsoleDemo to run it.

Parameters

-Alignment HorizontalAlignment optional
Left, Center or Right within the available width.
Possible values: Left, Center, Right
-BackgroundColor Object[] optional
Background colour per piece.
-Color Object[] optional
Foreground colour per piece: ConsoleColor names (exactly as Write-Host shows them: DarkRed is ANSI red, Red bright red; grey spells Gray), theme roles, #RRGGBB, rgb(r,g,b), r,g,b, or a markup palette name (cadetblue; palette.darkred for the palette colour where the bare name is a ConsoleColor).
-Decoration String[] optional
Decorations for the whole text: Bold, Dim, Italic, Underline, Strikethrough, Invert.
-Gradient Object[] optional
Two colours: the text is coloured from the first to the second (per character; steps on 16-colour terminals).
-Indent Int32 optional
Spaces before the text.
-LinesAfter Int32 optional
Empty lines after the text.
-LinesBefore Int32 optional
Empty lines before the text.
A hyperlink target for the whole text (OSC 8 where supported).
-LogFile String optional
A log file the text is appended to (plain text, see -LogFormat).
-Markup String optional
Markup added after the pieces ([bold lime]Ready[/] in [muted]3s[/]).
-NoNewLine SwitchParameter optional
Leaves the line open, so the next output continues on it.
-Role ThemeToken optional
A theme role for pieces without their own colour (Pass, Fail, Warn, Info, Muted, Accent...).
Possible values: Text, Muted, Subtle, Accent, Accent2, Pass, Fail, Warn, Info, Skip, Running, Pending, Border, Title, Header, Highlight, Surface, SurfaceAccent, Series1, Series2, Series3, Series4, Series5, Series6, Focus, Strong, Low, Critical, Gap
-ShowTimestamp SwitchParameter optional
Starts the line with the time in brackets.
-SplitLines String optional
Splits the text at this separator; the following lines are aligned under the first, after the time and prefix.
-Text Object[] optionalposition: 0pipeline: True (ByValue)
Text pieces, shown literally (strings, or rich text from New-ConsoleText).
-TimestampFormat String optional
The time format for -ShowTimestamp and the log (default yyyy-MM-dd HH:mm:ss).
-Width Int32 optional
The width for -Wrap and -Alignment (default the window width).
-Wrap SwitchParameter optional
Wraps long text at the window width, continuing under the text after the time and prefix.

Common Parameters

This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.

For more information, see about_CommonParameters.