diff --git a/src/MSBuild/TerminalLogger/TerminalLogger.cs b/src/MSBuild/TerminalLogger/TerminalLogger.cs
index bcae846e898..038ec536f6b 100644
--- a/src/MSBuild/TerminalLogger/TerminalLogger.cs
+++ b/src/MSBuild/TerminalLogger/TerminalLogger.cs
@@ -221,6 +221,8 @@ public ProjectContext(BuildEventContext context)
///
private bool _showCommandLine = false;
+ private uint? _originalConsoleMode;
+
///
/// Default constructor, used by the MSBuild logger infra.
///
@@ -263,6 +265,8 @@ public void Initialize(IEventSource eventSource, int nodeCount)
///
public void Initialize(IEventSource eventSource)
{
+ (_, _, _originalConsoleMode) = NativeMethodsShared.QueryIsScreenAndTryEnableAnsiColorCodes();
+
ParseParameters();
eventSource.BuildStarted += BuildStarted;
@@ -358,6 +362,8 @@ private bool TryApplyShowCommandLineParameter(string? parameterValue)
///
public void Shutdown()
{
+ NativeMethodsShared.RestoreConsoleMode(_originalConsoleMode);
+
_cts.Cancel();
_refresher?.Join();
Terminal.Dispose();