6

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?

silent
  • 14,494
  • 4
  • 46
  • 86
  • 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](https://stackoverflow.com/questions/45703539/fix-ansi-control-characters-before-powershell-output-to-a-file) as it may help you. – Krzysztof Madej Jan 15 '21 at 12:23
  • 1
    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://stackoverflow.com/a/58297270/13699968 – swbbl Jan 15 '21 at 15:51
  • Are the links above helpful? – Cece Dong - MSFT Jan 21 '21 at 09:48
  • I found it overly complicated tbh and just gave up on it for now – silent Jan 21 '21 at 09:49
  • This issue has been recorded on DevOps side, unfortunately there is not a perfect solution currently. You can follow this issue at following link: https://github.com/microsoft/azure-pipelines-agent/issues/1569. – Cece Dong - MSFT Jan 22 '21 at 08:55
  • thanks @CeceDong-MSFT, I subscribed to that thread – silent Jan 22 '21 at 09:02
  • See the answer in a previous question, [Fix ANSI control characters before PowerShell output to a file](https://stackoverflow.com/questions/45703539/fix-ansi-control-characters-before-powershell-output-to-a-file) – Dennis Mar 15 '23 at 16:22
  • Does this answer your question? [Fix ANSI control characters before PowerShell output to a file](https://stackoverflow.com/questions/45703539/fix-ansi-control-characters-before-powershell-output-to-a-file) – Dennis Mar 15 '23 at 16:23

1 Answers1

14

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.

Hilarion
  • 820
  • 7
  • 21