Reputation: 16198
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:
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:
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?
Upvotes: 9
Views: 5986
Reputation: 1617
It's exists a simple way if you are calling a bash azure task pwsh from linux, like this:
TERM=dumb pwsh yourscripthere.ps1
Upvotes: 1
Reputation: 870
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 aSystem.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.
Upvotes: 16