<p>I have a little c# console program that outputs some text using Console.WriteLine. I then pipe this output into a textfile like:</p>

<p><code>c:myprogram &gt; textfile.txt</code></p>

<p>However, the file is always an ansi text file, even when I start cmd with the /u switch. 
cmd /? says about the /u switch:</p>

<blockquote>
  <p>/U    Causes the output of internal
  commands to a pipe or file to be Unicode</p>
</blockquote>

<p>And it indeed makes a difference, when I do an </p>

<p><code>c:echo "foo" &gt; text.txt</code></p>

<p>the text.txt is unicode (without BOM)</p>

<p>I wonder why piping the output of my console program into a new file does not create an unicode file likewise and how i could change that? </p>

<p>I just use Windows Power Shell (which produces a unicode file with correct BOM), but I'd still like to know how to do it with cmd. </p>

<p>Thanks!</p>

The /U switch, as the documentation says, affects whether
internal
commands generate Unicode output. Your program is not one of cmd.exe's internal commands, so the /U option does not affect it.
To create a Unicode text file, you need to make sure your program is generating Unicode text.
Even that may not be enough, though. I came across
this blog from Junfeng Zhang
describing how to write Unicode text in a console program. It checks the file type of the standard output handle. For character files (a console or LPT port), it calls WriteFileW. For all other types of handles (including disk files and pipes), it converts the output string to the console's current code page. I'm afraid I don't know how that translates into .Net terms, though.