Powershell - disable colored command output
Asked Answered
M

2

8

I'm running a command (New-AzResourceGroupDeployment -WhatIf) in Powershell (or Powershell Core, both are options here), that produces some very colorful output like this:

enter image description here

The problem is that when I run this in an Azure DevOps pipeline, the logging there gets confused by the colors and produces lots of gibberish: enter image description here

So my question is: As this command itself does not have such an option, is there a general way in PowerShell (Core) to disable commands from producing colored output?

Mannequin answered 15/1, 2021 at 11:53 Comment(8)
There no easy way to remove ANSI color codes from powershell. You can check this topic - Fix ANSI control characters before PowerShell output to a file as it may help you.Feisty
Even if it's not the marked answer, use the second answer from the link @Krzysztof mentioned. That pattern matches ANSI Escape codes correctly (including the escape char itself). Direct link: https://mcmap.net/q/1324328/-fix-ansi-control-characters-before-powershell-output-to-a-fileGlandular
Are the links above helpful?Tiepolo
I found it overly complicated tbh and just gave up on it for nowMannequin
This issue has been recorded on DevOps side, unfortunately there is not a perfect solution currently. You can follow this issue at following link: github.com/microsoft/azure-pipelines-agent/issues/1569.Tiepolo
thanks @CeceDong-MSFT, I subscribed to that threadMannequin
See the answer in a previous question, Fix ANSI control characters before PowerShell output to a fileAirdry
Does this answer your question? Fix ANSI control characters before PowerShell output to a fileAirdry
A
16

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.

Aguste answered 16/12, 2021 at 17:29 Comment(0)
O
0

It's exists a simple way if you are calling a bash azure task pwsh from linux, like this:

TERM=dumb pwsh yourscripthere.ps1
Ovate answered 4/9 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.