With PowerShell 7.2 there's probably a new solution for you:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText;
From about_ANSI_Terminals document:
The following members control how or when ANSI formatting is used:
$PSStyle.OutputRendering
is a System.Management.Automation.OutputRendering
enum with the values:
- ANSI: ANSI is always passed through as-is
- PlainText: ANSI escape sequences are always stripped so that it is only plain text
- Host: This is the default behavior. The ANSI escape sequences are removed in redirected or piped output.
You can also try to query the PowerShell help system about it:
Get-Help -Name 'about_ANSI_Terminals';
A full example of use with something you have regardless of modules, etc. This:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::Ansi;
$PSVersionTable | Format-List;
will produce a colored output, wile this:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText;
$PSVersionTable | Format-List;
will produce plain-text.
If you try this:
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::Host;
$PSVersionTable | Format-Table | Tee-Object -FilePath '.\output.txt';
you should get a colored output in your terminal/console, but plain-text in the output.txt
file.
Also I've just found, that this was already answered here and described in much more detail.