Windows – Using Task Scheduler to Run a Task Before Shutdown

scheduled-taskssyncsynctoytask schedulerwindows 7

As of writing, the only options available are to "Begin the task":

  • On a schedule
  • At logon
  • At startup
  • On idle
  • On an event
  • At task modification/creation
  • On connect to user session
  • On disconnect from user session
  • On workstation lock
  • On workstation unlock

Is there a way to have Task Scheduler run a task before shutdown?

Best Answer

Even though this is an older thread, I'd like to add a solution I devised that works well.

I wanted to run a script or batch file whenever a shutdown or restart was initiated, but I didn't want to use Group Policies Shutdown Scripts function. The reason for this was pretty in depth, but I basically needed to run a script before certain services were closed.

I ended up creating a scheduled task as follows:

  • Type : On Event (Basic)
  • Log : System
  • Source : User32
  • EventID : 1074

When a user or command initiates a shutdown or restart as a logged on user or on a user's behalf, event ID 1074 will fire. By creating a task to use this to trigger a script, it will start the script and allow it to finish, however it will only report the task as "running" or "triggered" in the logs. I have not used this with a long script, so it may be worth testing further, but it works great for short scripts.

Related Question