knguyen knguyen - 1 year ago 101
PowerShell Question

In Powershell, how to run an executable for an hour, stop for half an hour, then repeat?

I have an executable at "C:\Users\Admin\Desktop\scraper\work.exe" and I'd like to do the following in Powershell.

  • Step 1: Run work.exe for an hour and stop

  • Step 2: Wait for half an hour

  • Repeat step 1

I hope there is a clean and simple solution. I have looked at Start-Process and Wait-Process with no success. Thanks.

Answer Source

You can start the process and grab the PID, then use that to stop it later. This will do what you asked forever (while 1).

While (1) {
    $proc = Start-Process C:\Users\Admin\Desktop\scraper\work.exe -PassThru
    Start-Sleep -Seconds (60*60)
    Stop-Process $proc.Id -force
    Start-Sleep -Seconds (30*60)