diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs index ff0117c6e9..a36bc096d1 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceProgress.cs @@ -3,6 +3,7 @@ using System; using System.Management.Automation; +using System.Management.Automation.Host; using System.Threading; using Dbg = System.Management.Automation.Diagnostics; @@ -10,7 +11,7 @@ using Dbg = System.Management.Automation.Diagnostics; namespace Microsoft.PowerShell { internal partial - class ConsoleHostUserInterface : System.Management.Automation.Host.PSHostUserInterface + class ConsoleHostUserInterface : PSHostUserInterface { /// /// Called at the end of a prompt loop to take down any progress display that might have appeared and purge any @@ -48,7 +49,7 @@ namespace Microsoft.PowerShell _pendingProgress = null; - if (SupportsVirtualTerminal && PSStyle.Instance.Progress.UseOSCIndicator) + if (SupportsVirtualTerminal && !PSHost.IsStdOutputRedirected && PSStyle.Instance.Progress.UseOSCIndicator) { // OSC sequence to turn off progress indicator // https://github.com/microsoft/terminal/issues/6700 @@ -99,7 +100,7 @@ namespace Microsoft.PowerShell { // Update the progress pane only when the timer set up the update flag or WriteProgress is completed. // As a result, we do not block WriteProgress and whole script and eliminate unnecessary console locks and updates. - if (SupportsVirtualTerminal && PSStyle.Instance.Progress.UseOSCIndicator) + if (SupportsVirtualTerminal && !PSHost.IsStdOutputRedirected && PSStyle.Instance.Progress.UseOSCIndicator) { int percentComplete = record.PercentComplete; if (percentComplete < 0) diff --git a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 index aa54a63dc0..e20534223f 100644 --- a/test/powershell/engine/Formatting/PSStyle.Tests.ps1 +++ b/test/powershell/engine/Formatting/PSStyle.Tests.ps1 @@ -237,6 +237,14 @@ Describe 'Tests for $PSStyle automatic variable' { $PSStyle.Progress.MaxWidth = $maxWidth } } + + It 'Do not use OSC indicator when the stdout is redirected' { + $pwsh = Join-Path $PSHOME 'pwsh' + + ## In the case that the stdout is redirected, pwsh should not write the OSC indicator Ansi sequence. + $result = & $pwsh -noprofile -Command { $PSStyle.Progress.UseOSCIndicator = $true; 'hello'} | Format-List + $result | Out-String -Stream | Should -BeExactly 'hello' + } } Describe 'Handle strings with escape sequences in formatting' {