title: Obtaining ExitCode using Start-Process and WaitForExit instead of -Wait
body: <p>I'm trying to run a program from PowerShell, wait for the exit, then get access to the ExitCode, but I am not having much luck. I don't want to use <code>-Wait</code> with <a href="https://technet.microsoft.com/en-us/library/hh849848.aspx" rel="noreferrer"><code>Start-Process</code></a>, as I need some processing to carry on in the background.</p>
  
  <p>Here's a simplified test script:</p>
  
  <pre><code>cd "C:\Windows"
  
  # ExitCode is available when using -Wait...
  Write-Host "Starting Notepad with -Wait - return code will be available"
  $process = (Start-Process -FilePath "notepad.exe" -PassThru -Wait)
  Write-Host "Process finished with return code: " $process.ExitCode
  
  # ExitCode is not available when waiting separately
  Write-Host "Starting Notepad without -Wait - return code will NOT be available"
  $process = (Start-Process -FilePath "notepad.exe" -PassThru)
  $process.WaitForExit()
  Write-Host "Process exit code should be here: " $process.ExitCode
  </code></pre>
  
  <p>Running this script will cause <a href="http://en.wikipedia.org/wiki/Notepad_%28software%29" rel="noreferrer">Notepad</a> to be started.  After this is closed manually, the exit code will be printed, and it will start again, without using <code>-wait</code>. No ExitCode is provided when this is quit:</p>
  
  <pre><code>Starting Notepad with -Wait - return code will be available
  Process finished with return code:  0
  Starting Notepad without -Wait - return code will NOT be available
  Process exit code should be here:
  </code></pre>
  
  <p>I need to be able to perform additional processing between starting the program and waiting for it to quit, so I can't make use of <code>-Wait</code>. How can I do this and still have access to the .ExitCode property from this process?</p>