Write-ConsoleText
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>]#All Parameter SetsDescription
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 @textOptionsColour two pieces and indent the message
Example 2
$textOptions = @{
Text = @('Maintenance complete')
Role = 'Pass'
Alignment = 'Center'
Width = 60
}
Write-ConsoleText @textOptionsCentre 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 @textOptionsA multi-line message aligned under the time stamp
Example 4
Get-ConsoleDemo -Slug 'prompt-tour' | Select-Object -ExpandProperty SourceRead 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
- Left, Center or Right within the available width.
- Possible values:
Left,Center,Right - -BackgroundColor Object[]
- Background colour per piece.
- -Color Object[]
- 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[]
- Decorations for the whole text: Bold, Dim, Italic, Underline, Strikethrough, Invert.
- -Gradient Object[]
- Two colours: the text is coloured from the first to the second (per character; steps on 16-colour terminals).
- -Indent Int32
- Spaces before the text.
- -LinesAfter Int32
- Empty lines after the text.
- -LinesBefore Int32
- Empty lines before the text.
- -Link String
- A hyperlink target for the whole text (OSC 8 where supported).
- -LogFile String
- A log file the text is appended to (plain text, see -LogFormat).
- -Markup String
- Markup added after the pieces ([bold lime]Ready[/] in [muted]3s[/]).
- -NoNewLine SwitchParameter
- Leaves the line open, so the next output continues on it.
- -Role ThemeToken
- 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
- Starts the line with the time in brackets.
- -SplitLines String
- Splits the text at this separator; the following lines are aligned under the first, after the time and prefix.
- -Text Object[]
- Text pieces, shown literally (strings, or rich text from New-ConsoleText).
- -TimestampFormat String
- The time format for -ShowTimestamp and the log (default yyyy-MM-dd HH:mm:ss).
- -Width Int32
- The width for -Wrap and -Alignment (default the window width).
- -Wrap SwitchParameter
- 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.