Powershell script scheduled task will not end

I want to use this Powershell script to automate some backups on Win7 machines. When run manually from the Powershell terminal the script runs as advertised and ends after a few seconds (it is a small backup).

The problem is when I try to automate the process with the Task Scheduler. When the time comes (or when I run the task manually) the script runs and performs the backup correctly. But it does not return to "Ready", even though I know it has ended. I have to stop the task manually each time.

Now, I have not waited to see what happens when the time comes for the task to run again, but I think it will not start a second time, because it is not "Ready".

What can I do about that?

UPDATE 1:

The Action I have chosen is "Start a program". In the "Program/script" box I have "powershell.exe" and as an argument I have the path to the script "*.ps1" file.

UPDATE 1:

I noticed that although the task does not return to "Ready", if I manually start it, it runs again.


The last 2 lines of BackupScript.ps1 are

Write-Host "Press any key to close ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Comment these out (or delete them) and scheduled jobs should complete and exit cleanly. If you really want the pause at the end, replace the last line with something like this to wrap a timeout around it (each sleep is 1 second):

$counter = 0
while(!$Host.UI.RawUI.KeyAvailable -and ($counter++ -lt 600))
{
      [Threading.Thread]::Sleep( 1000 )
}