title: How to wait and kill a timeout process in Powershell
body: <p>Using Powershell 2.0 in a Windows 7 desktop</p>
  
  <p>I want to create a process to run an ant command, and then wait for that process to finish in several minutes. If time out and the process still running then I want to kill it.</p>
  
  <p>I have wrote some code as below:</p>
  
  <pre><code>$p = Start-Process "cmd.exe" '/c ant execute' -PassThru -RedirectStandardOutput $log_file_path
  Wait-Process -id $p.ID -timeout 100
  if(!$p.hasExited) {
      echo "kill the process"
      $p.Kill()
      #Stop-Process $p.ID
  }
  
  Start-Sleep -s 30
  
  # continue to do other things
  </code></pre>
  
  <p>Although the process didn't get killed, it's still running even after the Start-Sleep statement get executed.</p>
  
  <p>I also tried with Stop-Process command, as the commented out line indicates, no luck, the process still running.</p>
  
  <p>I may have missed something important, please give a clue.</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>It turns out there are some child processes still running in the cmd window, kill only the cmd process is not enough.</p>
  
  <p>I finally get the job done with following code:</p>
  
  <pre><code>if(!$p.hasExited) {
      echo "kill the process"
      taskkill /T /F /PID $p.ID
      #$p.Kill()
      #Stop-Process $p.ID
  }
  </code></pre>