Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - disable colored command output

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?

like image 782
silent Avatar asked May 12 '26 19:05

silent


2 Answers

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.

like image 145
Hilarion Avatar answered May 14 '26 12:05

Hilarion


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

TERM=dumb pwsh yourscripthere.ps1
like image 43
Valtoni Boaventura Avatar answered May 14 '26 10:05

Valtoni Boaventura