title: Powershell waits on cmd.exe differently depending on environment
body: <p>Consider the powershell command:</p>
  
  <pre><code>cmd.exe "/c start notepad.exe"
  </code></pre>
  
  <p>Using powershell.exe (console) this command completes immediately after starting the cmd/notepad process and does not wait for notepad/cmd to exit. If I want it to wait for completion I have to pipe the output to Out-Null.</p>
  
  <p>Using the powershell ISE this command blocks execution until notepad/cmd is closed.</p>
  
  <p>Further, if I use create a new process (of powershell.exe) in a script running in powershell.exe using System.Diagnostics.Process and redirect standard output the script now blocks execution until notepad/cmd is closed. If I don't redirect output it does not block execution.</p>
  
  <p>But if I use c# to create a new process with the same settings/start info, and run the same script with redirected output it doesn't block execution.</p>
  
  <p>I'm at a loss here. Obviously it has something to do with the setup of the execution and output and maybe "cmd.exe". I'm hoping someone understands what's fundamentally happening behind the scenes differently in these cases. I know the ISE and cmd.exe isn't fully supported but the I don't know why the other 3 aren't consistent.</p>
  
  <p>Why do the scripts not run with the same behavior (especially the powershell console ones) and how do I get them to?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>After some troubleshooting I was able to get all the powershell.exe versions to run the same (the ISE isn't of importance to me). The odd ball where <code>cmd.exe "/c start notepad.exe"</code> blocks execution is when a new process is created in powershell to run powershell.exe using <code>System.Diagnostics.Process</code>. If output is redirected (the reason I was using <code>System.Diagnostics.Process</code> in the first place, <code>Start-Process</code> doesn't support redirecting except to a file) then calling <code>WaitForExit()</code> on the process object causes the blocking to occur. </p>
  
  <p>Simply substituting <code>WaitForExit()</code> with <code>Wait-Process</code> (and the process ID) causes the powershell script running in the new powershell.exe to execute as expected and exit after running the command. Hopefully this information combined with @mklement0 answer will be sufficient for anyone else having similar problems.</p>