Write-ConsoleText
Writes multicolour text to the console and, optionally, to a log file: Write-Color compatible, with markup, styles, gradients, padding, wrapping and structured logs.
Syntax
Write-ConsoleText [-BackGroundColor <Object[]>] [-Category <String>] [-Color <Object[]>] [-DateTimeFormat <String>] [-Encoding <String>] [-Gradient <Object[]>] [-HorizontalCenter] [-Level <String>] [-LinesAfter <Int32>] [-LinesBefore <Int32>] [-Link <String>] [-LogFile <String>] [-LogFormat <Text|Csv|JsonLines>] [-LogRetry <Int32>] [-LogSeparator <String>] [-LogTime <Boolean>] [-Markup <String>] [-NoConsoleOutput] [-NoNewLine] [-PadCenter <Int32>] [-PadCharacter <Char>] [-PadLeft <Int32>] [-PadRight <Int32>] [-Role <ThemeToken>] [-ShowTime] [-SplitLines <String>] [-StartSpaces <Int32>] [-StartTab <Int32>] [-Style <String[]>] [[-Text] <Object[]>] [-Width <Int32>] [-Wrap] [<CommonParameters>]#All Parameter SetsDescription
Writes multicolour text to the console and, optionally, to a log file: Write-Color compatible, with markup, styles, gradients, padding, wrapping and structured logs.
The parameters and aliases of Write-Color (PSWriteColor) work the same way: -Text pieces get -Color and -BackGroundColor per piece (pieces beyond the colours use the first colour), -StartTab, -StartSpaces, -LinesBefore, -LinesAfter, -ShowTime with -DateTimeFormat, -NoNewLine, -HorizontalCenter, -LogFile with -LogTime, -LogRetry and -Encoding, and -NoConsoleOutput. ConsoleColor names render exactly as Write-Host shows them. Beyond Write-Color: -Markup, -Style, -Link, -Role, hex and RGB colours, -Gradient, -PadLeft/-PadRight/-PadCenter with -PadCharacter (measured in terminal cells, so emoji and CJK pad correctly), -Wrap with a hanging indent under the time and prefix, -SplitLines with the following lines aligned under the first, and -LogFormat Text, Csv or JsonLines with -LogSeparator, -Level and -Category. Differences: the default colour is the terminal's own (Write-Color uses White), and a new log file is UTF-8 without a byte order mark by default (Write-Color writes UTF-16); appending to an existing log keeps its encoding, so Write-Color's UTF-16 logs stay UTF-16. In transcripts each call is one line; in jobs and remoting the colours travel with the output; redirected output is plain text. There is deliberately no Write-Color or wc alias.
Examples
EXAMPLE 1
Write-ConsoleText -BackGroundColor @('Value')Parameters
- -BackGroundColor Object[]
- Background colour per piece.
- -Category String
- A category for the log record.
- -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).
- -DateTimeFormat String
- The time format for -ShowTime and the log (default yyyy-MM-dd HH:mm:ss).
- -Encoding String
- The encoding of a new log file: utf8 without a byte order mark by default (Write-Color defaults to unicode, which is UTF-16 LE); Write-Color's names mean the same here (unicode, string and unknown are UTF-16 LE, bigendianunicode UTF-16 BE, utf32 UTF-32 LE), plus utf8BOM and utf8NoBOM. Appending to an existing file always keeps that file's encoding (detected from its byte order mark, or UTF-16 text without one), so a UTF-16 log from Write-Color stays UTF-16; when -Encoding names a different one, a warning says so.
- Possible values:
unknown,string,unicode,bigendianunicode,utf8,utf8BOM,utf8NoBOM,utf7,utf32,ascii,default,oem - -Gradient Object[]
- Two colours: the text is coloured from the first to the second (per character; steps on 16-colour terminals).
- -HorizontalCenter SwitchParameter
- Centres each line in the window (measured in terminal cells, so emoji and CJK centre correctly).
- -Level String
- A level for the log record (Info, Warning, Error...).
- -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).
- -LogFormat String
- The log format: Text (the Write-Color layout), Csv or JsonLines.
- Possible values:
Text,Csv,JsonLines - -LogRetry Int32
- Attempts to write the log file before giving up with a warning (default 2).
- -LogSeparator String
- The CSV separator (default a comma).
- -LogTime Boolean
- Whether log lines start with the time (default $true).
- -Markup String
- Markup added after the pieces ([bold lime]Ready[/] in [muted]3s[/]).
- -NoConsoleOutput SwitchParameter
- Writes only to the log file.
- -NoNewLine SwitchParameter
- Leaves the line open, so the next output continues on it.
- -PadCenter Int32
- Centres the text within this many cells.
- -PadCharacter Char
- The padding character (default a space).
- -PadLeft Int32
- Pads the text on the left to this many cells (right-aligned).
- -PadRight Int32
- Pads the text on the right to this many cells.
- -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 - -ShowTime 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.
- -StartSpaces Int32
- Spaces before the text.
- -StartTab Int32
- Tabs before the text (eight cells each).
- -Style String[]
- Decorations for the whole text: Bold, Dim, Italic, Underline, Strikethrough, Invert.
- -Text Object[]
- Text pieces, shown literally (strings, or rich text from New-ConsoleText).
- -Width Int32
- The width for -Wrap and -HorizontalCenter (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.