title: Get batch file return code in powershell
body: <p>I'm trying to write a PowerShell script as a "Framework". Basically what it does is calls batch files that installs msi files locally. </p>
  
  <p>There are few things I am trying to get done. </p>
  
  <ul>
  <li>PowerShell should get all exit codes whatever the batch files return. </li>
  <li>Need a timer and check if the batch file is stuck.If it's stuck kill it, return an error and continue</li>
  <li>Need a progress bar for each batch file installation. </li>
  </ul>
  
  <p>I'm not sure if I can do all or any. Since every package(the batch files) comes from different groups, I can't use PowerShell to do everything and have to use the batch files other groups sent me. </p>
  
  <p>I was able to get the packages install with a simple script but couldn't get to do any of the above things I was trying to do. I keep getting last exit code as 0. I assume it's because the batch file ran successfully.</p>
  
  <pre class="lang-psh prettyprint-override"><code>$APP1 = "Microsoft_RDP_8.1_L_EN_01" 
  Stop-Process -Name reg* -Force
  Write-Host "=== $Time -- Starting $APP1 Installation"
  TRY
      {
          $Install = "$pwd\cmd\$App1\Install.cmd"
          cmd /c $Install /b 1
          $App1ErrorCode = $LastExitCode
          Write-Host "Final Return Code Of $APP1 Is = $APP1ErrorCode"
      }
  CATCH
      {
          Write-Host "-----------------------------------------------------------" -ForegroundColor Green
          Write-Host "Caught an exception:" -ForegroundColor Red
          Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
          Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
          Write-Host "-----------------------------------------------------------" -ForegroundColor Green
      }
  
  $LastExitCode = $null   
  $Value = $null
  </code></pre>