title: Console.OutputEncoding breaks batch files
body: <p>On a Windows 2008 (non-R2) server, I experience the same behaviour as <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/e448b241-e250-4dcb-8ecd-361e00920dde/consoleoutputencoding-breaks-batch-files?forum=netfxbcl" rel="nofollow">described in this MSDN forum posting</a>:</p>
  
  <blockquote>
    <p>Whenever I run a .NET Console application that changes the <a href="https://msdn.microsoft.com/en-us/library/system.console.outputencoding" rel="nofollow"><code>Console.OutputEncoding</code></a> to UTF-8, no more batch files can be executed until I close and reopen the CMD window.</p>
  </blockquote>
  
  <p>This only happens on Windows 2008 (non-R2), this does not happen on Windows 2008 R2 or Windows 2012 servers.</p>
  
  <p>I initially discovered this when my <a href="http://www.csscript.net/" rel="nofollow">CS-Script</a> files "broke" my backup batch files.</p>
  
  <p>As soon as the .NET Console application exits, running further batch files simply immediately return, no matter whether they simple <code>echo</code> a text or <code>start</code> some programs.</p>
  
  <p>This is the minimum program to generate the erroneous behaviour:</p>
  
  <pre><code>namespace utf8
  {
      using System;
      using System.Text;
  
      internal static class Program
      {
          private static void Main()
          {
              Console.OutputEncoding = Encoding.UTF8;
          }
      }
  }
  </code></pre>
  
  <p>I'm completely clueless what's going on here and how to fix it.</p>
  
  <p><strong>My question:</strong></p>
  
  <ul>
  <li>Any idea why setting the <code>Console.OutputEncoding</code> breaks further batch processing?</li>
  <li>Any hints on how to solve this (if I have/don't have access to the .NET Console sources)?</li>
  </ul>
  
  <p><strong>Update 1:</strong></p>
  
  <p>For my own code, this seems to fix it:</p>
  
  <pre><code>namespace utf8
  {
      using System;
      using System.Text;
  
      internal static class Program
      {
          private static void Main()
          {
              var before = Console.OutputEncoding;
              Console.OutputEncoding = Encoding.UTF8;
              try
              {
  
                  // ... Do some processing...
  
              }
              finally
              {
                  Console.OutputEncoding = before;
              }
          }
      }
  }
  </code></pre>
  
  <p>I.e. after running the above code, the current CMD window still <em>is</em> able to run batch files.</p>
  
  <p><strong>Update 2:</strong></p>
  
  <p>If anyone ever comes here with CS-Script behaving like stated above; <a href="https://csscriptsource.codeplex.com/releases/view/614649" rel="nofollow">version 3.9.8.0 of CS-Script</a> fixes this. So simply update to 3.9.8.0 or newer.</p>