title: spawn console processes with redirected standard handles in powershell
body: <p>I have started a process using System.Diagnostics.Process::start() with redirected stdi/o. This process creates another process. I want to redirect the stdi/o of the second process in powershell.</p>
  
  <p>Here is a sample code in which i am running a perl script from cmd.exe process created using System.Diagnostics.Process::start(). I want to see the output of the perl script in the log file as well as send key strokes to the perl interpreter.</p>
  
  <p>Powershell script:</p>
  
  <pre><code>$LogFilePath = ((get-location).ToString() + "\Log.txt");
  "test stdio redirection of child process" &gt; $LogFilePath;
  $cmdProcess       = New-Object System.Diagnostics.Process;
  $cmdProcess.StartInfo.FileName       = "cmd.exe"    
  $cmdProcess.StartInfo.UseShellExecute = $false;       
  $cmdProcess.StartInfo.RedirectStandardInput = $true;
  $cmdProcess.StartInfo.RedirectStandardOutput = $true;
  $cmdProcess.StartInfo.RedirectStandardError = $true;    
  Register-ObjectEvent $cmdProcess ErrorDataReceived -SourceIdentifier "cmdProcess.ErrorDataReceived" -Action {  if(![string]::IsNullOrEmpty($EventArgs.Data)) { $EventArgs.Data | Add-Content $global:LogFilePath } } 
  Register-ObjectEvent $cmdProcess OutputDataReceived -SourceIdentifier "cmdProcess.OutputDataReceived" -Action { if(![string]::IsNullOrEmpty($EventArgs.Data)) { $EventArgs.Data | Add-Content $global:LogFilePath } } 
  $cmdProcess.start()
  Start-Sleep -s 2 ;
  $cmdProcess.BeginErrorReadLine();
  Start-Sleep -s 2 ;
  $cmdProcess.BeginOutputReadLine();
  Start-Sleep -s 2 ;
  $cmdProcess.StandardInput.WriteLine("D:\batch_files\CC_Automation\test\hello.pl") ;
  </code></pre>
  
  <p>hello.pl:</p>
  
  <pre><code>print "Hello\n";
  print "press Enter key to exit";
  $key = &lt;&gt;;
  </code></pre>
  
  <p>Output:Log.txt</p>
  
  <blockquote>
    <p>test stdio redirection of child process Microsoft Windows [Version
    6.1.7601] Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    C:\Windows\System32\WindowsPowerShell\v1.0>D:\batch_files\CC_Automation\test\hello.pl</p>
  </blockquote>
  
  <p>I have found something similar in "C" : <a href="https://support.microsoft.com/en-us/kb/190351" rel="nofollow">https://support.microsoft.com/en-us/kb/190351</a></p>