Issue
I want to make the font as bold for the path that I'm printing using Write-Host
. I'm flexible to using other methods like echo or something else.
I've tried other methods like Write-Debug
, etc, also checked the module WindowsConsoleFonts
.
But none of them supports font properties like making them bold or italic while printing them.
$pathString = "[" + (Get-Location) + "]"
Write-Host $pathString -ForegroundColor Cyan
I'm using PowerShell 5.1 which doesn't support MarkDown rendering, else I would have done it using Markdown.
Solution
You can achieve bold text via VT (Virtual Terminal) escape sequences.
However, regular Windows console windows (conhost.exe
) do not support italics, and neither does their upcoming successor, Windows Terminal (at least as of this writing).[1]
In recent versions of Windows 10, support for VT sequences is enabled by default in both Windows PowerShell and PowerShell Core.
However, Write-Host
has no support for them, so you must embed the escape sequences directly into the strings you pass to Write-Host
(or strings you send to the success output stream, if it goes to the console):
Note:
I'm omitting
Write-Host
from the examples below, because it isn't strictly necessary, but colored text generally should indeed be written to the display (host), not to the success output stream.While it is better to consistently use VT sequences for all formatting needs - including colors - it is possible to combine them with
Write-Host -ForegroundColor /
-BackgroundColor`.
PowerShell Core:
PowerShell Core supports embedding escape sequences directly in "..."
(double-quoted strings), via the `e
escape sequence, which expands to a literal ESC character, which is the character that initiates a VT escape sequence.
"To `e[1mboldly`e[m go ..., in `e[36mcyan`e[m."
Windows PowerShell:
There's no support for `e
; the easiest solution is to use \e
as placeholders and use -replace
to substitute actual ESC characters (Unicode code point 0x1b
) for them:
"To \e[1mboldly\e[m go ..., in \e[36mcyan\e[m." -replace '\\e', [char] 0x1b
[1] From PowerShell Core, you can run the following test command to see if the word italics
prints in italics: "`e[3mitalics`e[m after"
Note that italics in the terminal are supported on macOS and at least in some Linux distros; e.g., Ubuntu 18.04
Answered By - mklement0 Answer Checked By - Terry (WPSolving Volunteer)