How to add an option to stop a scheduled task

Page 1 of 2 12 LastLast

  1. Posts : 89
    Windows 10/64
       #1

    How to add an option to stop a scheduled task


    I searched the web with every word combination I can think of and haven't found an answer to my question.

    Is there a way to add an option to a scheduled task to give the user an option, before the task runs, to cancel that task until the next scheduled time, or even better, delay it for a specific time period?

    I assume it would have something to do with adding the correct action or argument the actions section of the task in task scheduler or running a bat file to accomplish the entire event (If a bat file any help with how to write it or a link to a website with instructions would be appreciated).
      My Computer


  2. Posts : 4,187
    Windows 11 Pro, 22H2
       #2

    It sounds to me like you have a good handle on this already. I have to admit that I have not done this myself, but it would make sense to me to run a batch file that will interact with the user and require a response before continuing. Based upon the response or lack thereof, you could simply elect not to run the scheduled task, or you could modify the task from the command line.

    You can run SCHTASKS /? for help with the command line syntax.
      My Computers


  3. Posts : 1,223
    W10-Pro 22H2
       #3

    A batch file seems the easiest way. To enter data into the batch file when it runs, you set an environmet variable like this:
    Code:
    set /p id=hit Y to run, N to stop:
    where the env variable that results is called id. Everything after the '=' is just a prompt for the user, so
    Code:
    set /p id=
    would work just as well, but with a blank line facing the user. Then you need to test what id contains.

    You can add a delay to a batch file with
    Code:
    timeout /t 5
    which waits for 5 seconds. Is that any help?
      My Computer


  4. Posts : 745
    Windows 10/11
       #4

    If you decide to use a script with a prompt, consider using VBScript (i.e. MsgBox command) instead of a batch file. It will look much better and the user can interact by clicking with a mouse. And there's no need to do a hybrid batch file with VBS code. It can all be done in one tidy VBS script. Let me know if you need assistance or sample code
      My Computer


  5. Posts : 89
    Windows 10/64
    Thread Starter
       #5

    LesFerch said:
    If you decide to use a script with a prompt, consider using VBScript (i.e. MsgBox command) instead of a batch file. It will look much better and the user can interact by clicking with a mouse. And there's no need to do a hybrid batch file with VBS code. It can all be done in one tidy VBS script. Let me know if you need assistance or sample code
    That would be great if you have the time.

    What I'm trying to do is automate a daily reboot of all my PCs (they all get turned off every night, but since Microsoft has decided that Windows will only "reset" after a restart, not a shutdown, I've noticed my PCs starting to get "sluggish" after about a week without a restart.

    I have created a scheduled task using the shutdown.exe command with the added arguments "-r -f -t 150" which gives me a popup telling me the PC will restart is 2 minutes so I have time to close what I'm doing, but I would prefer the option to either skip that restart (or even better tell it to delay the restart for some time period).
      My Computer


  6. Posts : 4,187
    Windows 11 Pro, 22H2
       #6

    sewing1243 said:
    since Microsoft has decided that Windows will only "reset" after a restart, not a shutdown, I've noticed my PCs starting to get "sluggish" after about a week without a restart.
    By any chance are you referring to fast startup here? That can be disabled as the tutorial below describes.

    In fact, I've disabled it on all my machines because I use Wake on LAN to power on machines on demand for certain tasks and fast startup interferes with WOL.

    EDIT: Sorry, forgot the link

    Turn On or Off Fast Startup in Windows 10
      My Computers


  7. Posts : 89
    Windows 10/64
    Thread Starter
       #7

    hsehestedt said:
    By any chance are you referring to fast startup here? That can be disabled as the tutorial below describes.

    In fact, I've disabled it on all my machines because I use Wake on LAN to power on machines on demand for certain tasks and fast startup interferes with WOL.
    Fast startup is disabled on all my PCs. As far as I can tell Microsoft has never resolved (I'm not sure that they have ever tried) the multiple memory leaks Windows in all versions has always had, not to mention all the clutter many apps leave behind when they are "closed". Despite the theory that turning off fast start should clear everything after a shutdown and power on, it has been my experience it doesn't. As far as I can tell only a Restart will completely shutdown the kernel and clear all the garbage out.
      My Computer


  8. Posts : 745
    Windows 10/11
       #8

    sewing1243 said:
    That would be great if you have the time.

    What I'm trying to do is automate a daily reboot of all my PCs (they all get turned off every night, but since Microsoft has decided that Windows will only "reset" after a restart, not a shutdown, I've noticed my PCs starting to get "sluggish" after about a week without a restart.

    I have created a scheduled task using the shutdown.exe command with the added arguments "-r -f -t 150" which gives me a popup telling me the PC will restart is 2 minutes so I have time to close what I'm doing, but I would prefer the option to either skip that restart (or even better tell it to delay the restart for some time period).
    No problem. I'll post some code tonight.
      My Computer


  9. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #9

    This batch script fits your requirements but I appreciate that you might decide to use vbs msgboxes as Les suggests:-

    Code:
    set /p id=hit Y to run, N to stop:
    If /I "%id%"=="y" shutdown -r -f -t 150
    The first line is mngerhold's simple one-liner that was posted earlier.


    All the best,
    Denis
      My Computer


  10. Posts : 745
    Windows 10/11
       #10

    sewing1243 said:
    I have created a scheduled task using the shutdown.exe command with the added arguments "-r -f -t 150" which gives me a popup telling me the PC will restart is 2 minutes so I have time to close what I'm doing, but I would prefer the option to either skip that restart (or even better tell it to delay the restart for some time period).
    I wrote two scripts for you to try.

    The first one is ShutdownDelay.vbs. It will display this prompt:
    How to add an option to stop a scheduled task-shutdowndelayvbs.png
    To use this script, replace your current scheduled task command line with something similar to this:
    WScript.exe C:\Myscripts\ShutdownDelay.vbs

    It's short and simple, but it has the following limitations: 1) After setting a delay time and clicking OK, it will show nothing on the screen to indicate that a shutdown (or restart) is going to occur. It just silently sleeps for the specified time and then executes shutdown.exe, 2) It has no unattended mode. That is, the prompt will stay on the screen forever. There is no option for it to continue and run shutdown.exe if no one is there to answer the prompt.

    To address those limitations, I wrote ShutdownDelay.hta. It will display this prompt:
    How to add an option to stop a scheduled task-shutdowndelayhta.png
    To use this script, replace your current scheduled task command line with something similar to this:
    MSHTA.exe C:\Myscripts\ShutdownDelay.hta
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:09.
Find Us




Windows 10 Forums