Using Task Scheduler to Run a Task Before Shutdown
Solution 1
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.
Solution 2
You can create shutdown tasks with the Group Policy Editor in Windows 7.
Solution 3
To do this you will need to set up a custom event filter in Task Scheduler.
Triggers > New > Custom > Edit Event > XML
and paste the following:
<QueryList>
<Query Id="0" Path="System">
<Select Path="System">
*[System[Provider[@Name='User32'] and (Level=4 or Level=0) and (EventID=1074)]]
and
*[EventData[Data[@Name='param5'] and (Data='power off')]]
</Select>
</Query>
</QueryList>
This will filter out the power off event only.
If you look in the event viewer you can see under Windows Logs > System under Details tab>XML View that there's this.
- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="User32" Guid="{xxxxx-xxxxxxxxxxx-xxxxxxxxxxxxxx-x-x}" EventSourceName="User32" />
<EventID Qualifiers="32768">1074</EventID>
<Version>0</Version>
<Level>4</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x8080000000000000</Keywords>
<TimeCreated SystemTime="2021-01-19T18:23:32.6133523Z" />
<EventRecordID>26696</EventRecordID>
<Correlation />
<Execution ProcessID="1056" ThreadID="11288" />
<Channel>System</Channel>
<Computer>DESKTOP-REDACTED</Computer>
<Security UserID="x-x-x-xx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxx" />
</System>
- <EventData>
<Data Name="param1">Explorer.EXE</Data>
<Data Name="param2">DESKTOP-REDACTED</Data>
<Data Name="param3">Other (Unplanned)</Data>
<Data Name="param4">0x0</Data>
<Data Name="param5">power off</Data>
<Data Name="param6" />
<Data Name="param7">DESKTOP-REDACTED\username</Data>
</EventData>
</Event>
You can test the query with the query list code above in the event viewer by clicking
Create Custom View... > XML > Edit query manually
and pasting the code, giving it a name Power Off Events Only before you try it in the Task Scheduler.
Solution 4
Task Scheduler is limited in its ability to schedule a task at shutdown. A similar question was asked on Stack Overflow (how-to-schedule-a-task-to-run-when-shutting-down-windows), and the answers there describe several methods other than using the Task Manager, including the Group Policy Editor method, which is described in detail and might be a better way to handle it.
The Task Scheduler can be used instead of Group Policy Editor. However, it's only good for very short tasks, which will run as long as the system is restarting or shutting down, which is usually only a few seconds.
In addition, please note that the task status can be:
The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist. (0x800704DD)
However, it doesn't mean that it didn't run.
Related videos on Youtube
Tigs
Updated on September 17, 2022Comments
-
Tigs over 1 year
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?
-
Oz Edri over 9 yearsYou are right, and I wish I could do it right now since I don't know when and if I'll get enough credit. If it's too problematic please delete my answer and add it as a comment under your name. Although I won't get any recognition, the community will get this information - and that's the first priority :-)
-
Rsya Studios over 9 years+1 to help you get enough rep, you can post it as a comment once you reach the threshold :)
-
Marc.2377 over 4 yearsHow? The instructions should be included here.
-
Scrat Sober about 4 yearsthis isn't working in server core 1909, I think because the scheduled task service is stopped before this event is logged.. so far I haven't found an event that works instead
-
Feriman about 4 yearsIt doesn't work on Windows 10 Home.
-
CrouZ over 3 yearsI got this to work on Windows 10 Pro N version 1909, but I also had to change: General (tab) / Security options (frame) / Select "Run whether user is logged on or not". The default, "Run only when user is logged on", did not work for me.
-
CrouZ over 3 years@Marc.2377 Run (win+R) gpedit.msc: Computer Configuration / Windows Settings / Scripts (Startup/Shutdown) / Shutdown. In the Shutdown Properties dialog, add the scripts to execute on shutdown.
-
Tilo about 2 yearsWin 2019 Server didn't work. Win 2008 R2 worked.