New-ConsoleText
Creates rich text: multicolour words for table cells, calculated properties, titles, labels, log lines or output on its own.
Syntax
New-ConsoleText
[-Alignment <Left| Center| Right>]
[-BackgroundColor <Object[]>]
[-Color <Object[]>]
[-Decoration <String[]>]
[-Gradient <Object[]>]
[-Link <String>]
[-Markup <String>]
[-Overflow <Wrap| Ellipsis| Clip>]
[-Role <ThemeToken>]
[[-Text] <Object[]>]
[<CommonParameters>]#All Parameter SetsDescription
-Text takes pieces that are shown literally (data is never interpreted); -Color and -BackgroundColor take one colour per piece, and pieces beyond the colours use the first colour, as Write-Color does. Colours are ConsoleColor names exactly as Write-Host shows them, theme roles (Pass, Fail, Warn, Info, Muted, Accent) that follow the light or dark theme, #RRGGBB, rgb(r,g,b) or r,g,b. -Markup adds Spectre-compatible markup ([bold lime]Ready[/]); -Decoration adds Bold, Dim, Italic, Underline, Strikethrough or Invert; -Link makes the text a hyperlink where the terminal supports it; -Gradient colours the text from one colour to another. The result works as a cell value (also from a calculated property), as the title or label of any widget, with Write-ConsoleLogEntry and Update-Console, and with Show-Console. Without colour (NO_COLOR, redirected output) the text stays plain.
Examples
Example 1
$tableOptions = @{
Property = @('Name', @{
Name = 'Status'
Expression = {
$textOptions = @{
Text = @($_.Status)
Role = $_.Status
Decoration = @('Bold')
}
New-ConsoleText @textOptions
}
})
}
@(
[pscustomobject]@{ Name = 'web01'; Status = 'Pass' }
[pscustomobject]@{ Name = 'web02'; Status = 'Fail' }
) | Show-ConsoleTable @tableOptionsBuild a rich text cell for each row. The table evaluates Expression with the current row as $_. Text receives a one-element array containing that row's Status. Role uses the same value to pick the theme color, and Decoration makes the cell bold.
Example 2
$textOptions = @{
Text = @('SRV', '01', ' online')
Color = @('Cyan', 'Yellow', 'Green')
}
New-ConsoleText @textOptions | Show-ConsoleColour separate text pieces. The Text and Color arrays pair by position: SRV is cyan, 01 is yellow, and the final piece is green. Spaces are part of the text pieces, so the leading space in the last entry separates online from the identifier.
Example 3
New-ConsoleText -Markup '[bold]ConsoleMonster[/] [muted]ready[/]';
$textOptions = @{
Text = @('ConsoleMonster')
Gradient = @('#00c6ff', '#ff00aa')
}
New-ConsoleText @textOptionsCreate two rich text values. Markup interprets the bold and muted tags. Gradient uses the first color as the starting point and the second as the end, across the characters of ConsoleMonster.
Example 4
Get-ConsoleDemo -Slug 'welcome-compact' | Select-Object -ExpandProperty Source
Get-ConsoleDemo -Slug 'everyday-formatting' | Select-Object -ExpandProperty SourceRead any of these gallery scripts to see New-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
- Alignment when the text is written as a widget: Left, Center or Right.
- Possible values:
Left,Center,Right - -BackgroundColor Object[]
- Background colour per piece.
- -Color Object[]
- Foreground colour per piece: ConsoleColor names (PowerShell mapping), theme roles, #RRGGBB, rgb(r,g,b) or r,g,b.
- -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.
- -Link String
- A hyperlink target for the whole text (OSC 8 in Windows Terminal and VS Code, plain text elsewhere).
- -Markup String
- Markup added after the pieces ([bold lime]Ready[/] in [muted]3s[/]).
- -Overflow TextOverflow
- Overflow policy when rendered in constrained cells.
- Possible values:
Wrap,Ellipsis,Clip - -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 - -Text Object[]
- The text pieces, shown literally (strings, or rich text from New-ConsoleText).
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.