You mention powershell
(powershell.exe
), i.e. the CLI of Windows PowerShell.
Windows PowerShell (unlike PowerShell (Core) 7+, see below) itself does not use coloring / formatting based on VT / ANSI escape sequences.
The implication is that third-party code is producing the VT sequences in your case, so you must deactivate (or reconfigure) it to avoid such sequences in the output.
A prime candidate is a custom prompt
function; such functions often involve coloring for a better command-line experience.
In programmatic use of powershell.exe
, however, you would only see what the prompt function prints if you feed PowerShell commands to the CLI's stdin, accompanied by passing argument -File -
to the CLI (to instruct it to read commands from stdin) or by default.
To exclude the prompt-function output from the output altogether, use -Command -
, as discussed in the answer to your previous question.
If you do want it, but want to use the default prompt string, suppress $PROFILE
loading with the -NoProfile
parameter, which is generally preferable in programmatic processing.
Controlling use of colored output (VT / ANSI escape sequences) in PowerShell (Core) 7.2+
In PowerShell (Core) 7+ (pwsh.exe
) - but not in Windows PowerShell (powershell.exe
) - PowerShell itself situationally uses VT (ANSI) escape sequence to produce formatted/colored output, such as in the output of Select-String
and, in v7.2+, in formatted output in general, notably column headers in tabular output / property names in list output.
Note that third-party PowerShell code that uses VT sequences, especially if it predates PowerShell (Core) 7+, may not respect any of the standard mechanisms described above for disabling them (though may conceivably offer a custom mechanism).
[1] This applies since v7.2.0-preview.9, where Host
was made the default and the previous default, Automatic
, was removed altogether. In preview versions of v7.3.0, Ansi
was temporarily the default, but since the official v7.3.0 release the (sensible) default is again Host
.
[2] Notably, this means that string data that has embedded ANSI / VT escape sequences is not subject to $PSStyle.OutputRendering
in v7.3+ (a change from v7.2), because strings aren't handled by the formatting system (they print as-is).