title: powershell behavior when host file handle is closed
body: <p>I'm looking for an explanation (preferably documentation) of the following madness.</p>
  
  <p>Say I have the following simple Perl script:</p>
  
  <pre><code>use strict;
  use warnings;
  
  my $output = "C:\\Temp\\aout.txt";
  my $outpute = "C:\\Temp\\aoute.txt";
  my $command = "powershell C:\\test.ps1";
  
  close STDOUT;
  close STDERR;
  if ( (my $pid = fork()) == 0 ) {
      open (STDOUT,"&gt;&gt;$output") or die "cannot open $output as stdout: $!";
      open (STDERR,"&gt;&gt;$outpute") or die "cannot open $outpute as stderr: $!";
      exec $command or die "couldn't exec $command: $!";
  } else {
      my $ret = waitpid($pid,0);
  }
  </code></pre>
  
  <p>And that the Powershell script contains:</p>
  
  <pre><code>write-output "yay1";
  write-error "nay2";
  write-output "yay3";
  write-error "nay4";
  write-output "yay5";
  write-error "nay6";
  write-output "yay7";
  write-error "nay8";
  write-host "done!";
  </code></pre>
  
  <p>If I run the Perl script, it produces two files, as expected, with the following output:</p>
  
  <p><strong>aout.txt:</strong></p>
  
  <pre><code>yay1
  yay3
  yay5
  yay7
  done!
  </code></pre>
  
  <p><strong>aoute.txt:</strong></p>
  
  <pre><code>C:\test.ps1 : nay2
  At line:1 char:41
  + C:\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  C:\test.ps1 : nay4
  At line:1 char:41
  + C:\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  C:\test.ps1 : nay6
  At line:1 char:41
  + C:\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  C:\test.ps1 : nay8
  At line:1 char:41
  + C:\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  </code></pre>
  
  <p>If I modify the Perl script to keep STDERR closed before executing Powershell:</p>
  
  <pre><code>use strict;
  use warnings;
  
  my $output = "C:\\Temp\\aout.txt";
  my $command = "powershell C:\\test.ps1";
  
  close STDOUT;
  close STDERR;
  if ( (my $pid = fork()) == 0 ) {
      open (STDOUT,"&gt;&gt;$output") or die "cannot open $output as stdout: $!";
      exec $command or die "couldn't exec $command: $!";
  } else {
      my $ret = waitpid($pid,0);
  }
  </code></pre>
  
  <p>Then it produces one STDOUT file with <em>all</em> of the output:</p>
  
  <pre><code>yay1
  C:\\test.ps1 : nay2
  At line:1 char:41
  + C:\\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  yay3
  C:\\test.ps1 : nay4
  At line:1 char:41
  + C:\\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  yay5
  C:\\test.ps1 : nay6
  At line:1 char:41
  + C:\\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  yay7
  C:\\test.ps1 : nay8
  At line:1 char:41
  + C:\\test.ps1 &lt;&lt;&lt;&lt; 
      + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
      + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
  
  done!
  </code></pre>
  
  <p><strong>Is Powershell really realizing that STDERR is closed, and is doing a clean redirect onto STDOUT?  Or is it the command line console?  Or is it Perl?</strong></p>
  
  <p>For reference, this is on Windows Server 2003 x64 SP2 with:</p>
  
  <pre><code>C:\&gt;perl --version
  
  This is perl, v5.8.8 built for MSWin32-x64-multi-thread
  (with 33 registered patches, see perl -V for more detail)
  
  Copyright 1987-2006, Larry Wall
  
  Binary build 819 [267479] provided by ActiveState http://www.ActiveState.com
  Built Aug 27 2006 22:13:23
  
  Perl may be copied only under the terms of either the Artistic License or the
  GNU General Public License, which may be found in the Perl 5 source kit.
  
  Complete documentation for Perl, including FAQ lists, should be found on
  this system using "man perl" or "perldoc perl".  If you have access to the
  Internet, point your browser at http://www.perl.org/, the Perl Home Page.
  
  
  C:\&gt;powershell get-host
  
  
  Name             : ConsoleHost
  Version          : 2.0
  InstanceId       : 1ac67afa-f020-414c-b47b-031bdc8cc703
  UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
  CurrentCulture   : en-US
  CurrentUICulture : en-US
  PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
  IsRunspacePushed : False
  Runspace         : System.Management.Automation.Runspaces.LocalRunspace
  </code></pre>
  
  <p>and this is part of an ongoing effort related to <a href="https://stackoverflow.com/questions/8978052/powershell-2-0-redirection-file-handle-exception">this</a> and <a href="https://stackoverflow.com/questions/9065296/powershell-2-0-command-line-redirection">this</a>.</p>