How to schedule a task to run when shutting down windows

WindowsWindows Xp

Windows Problem Overview


How do you schedule a task in Windows XP to run when you shutdown windows. Such that I want to run a simple command line program I wrote in c# everytime I shut down windows. There doesn't seem to be an option in scheduled tasks to perform this task when my computer shuts down.

Windows Solutions


Solution 1 - Windows

Execute gpedit.msc (local Policies)

Computer Configuration -> Windows settings -> Scripts -> Shutdown -> Properties -> Add

Solution 2 - Windows

In addition to Dan Williams' answer, if you want to add a Startup/Shutdown script, you need to be looking for Windows Settings under Computer Configuration. If you want to add a Logon/Logoff script, you need to be looking for Windows Settings under User Configuration.

So to reiterate what Dan said with this information included,

For Startup/Shutdown:

  1. Run gpedit.msc (Local Policies)
  2. Computer Configuration -> Windows Settings -> Scripts -> Startup or Shutdown -> Properties -> Add

For Logon/Logoff:

  1. Run gpedit.msc (Local Policies)
  2. User Configuration -> Windows Settings -> Scripts -> Logon or Logoff -> Properties -> Add

Source: http://technet.microsoft.com/en-us/library/cc739591(WS.10).aspx

Solution 3 - Windows

For those who prefer using the Task Scheduler, it's possible to schedule a task to run after a restart / shutdown has been initiated by setting the task to run after event 1074 in the System log in the Event Viewer has been logged. However, it's only good for very short task, which will run as long as the system is restarting / shutting down, which is usually only a few seconds.

  • From the Task Scheduler:
    >Begin the task: On an event
    Log: System
    Source: USER32
    EventID: 1074
  • From the command prompt:
    >>schtasks /create /tn "taskname" /tr "task file" /sc onevent /ec system /mo *[system/eventid=1074]

Comment: the /ec option is available from Windows Vista and above. (thank you @t2d)

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.

Solution 4 - Windows

One workaround might be to write a simple batch file to run the program then shutdown the computer.

You can shut down from the command line -- so your script could be fairly simple:

c:\directory\myProgram.exe
C:\WINDOWS\system32\shutdown.exe -s -f -t 0

Solution 5 - Windows

If you run GPEdit.MSC you can go to Computer Configuration -> Windows Settings -> Scripts, and add startup /shutdown scripts. These can be simple batch files, or even full blown EXEs. Also you can adjust user configurations for logon and logoff scripts in this same tool. This tool is not available in WIndows XP Home.

Solution 6 - Windows

The Group Policy editor is not mentioned in the post above. I have used GPedit quite a few times to perform a task on bootup or shutdown. Here are Microsoft's instructions on how to access and maneuver GPedit.

How To Use the Group Policy Editor to Manage Local Computer Policy in Windows XP

Solution 7 - Windows

What I can suggest doing is creating a shortcut to the .bat file (for example on your desktop) and a when you want to shutdown your computer (and run the .bat file) click on the shortcut you created. After doing this, edit the .bat file and add this line of code to the end or where needed:

c:\windows\system32\shutdown -s -f -t 00

What this does it is

  1. Runs the shutdown process
  2. Displays a alert
  3. Forces all running processes to stop
  4. Executes immediately

Solution 8 - Windows

I posted this answer too over on superuser.


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 9 - Windows

You can run a batch file that calls your program, check out the discussion here for how to do it: http://www.pcworld.com/article/115628/windows_tips_make_windows_start_and_stop_the_way_you_want.html

(from google search: windows schedule task run at shut down)

Solution 10 - Windows

On Windows 10 Pro, the batch file can be registered; the workaround of registering cmd.exe and specifying the bat file as a param isn't needed. I just did this, registering both a shutdown script and a startup (boot) script, and it worked.

Solution 11 - Windows

I had to also enable "Specify maximum wait time for group policy scripts" and "Display instructions in shutdown scripts as they run" to make it work for me as I explain here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCrippeobladeView Question on Stackoverflow
Solution 1 - WindowsDan WilliamsView Answer on Stackoverflow
Solution 2 - WindowsBrett PenningsView Answer on Stackoverflow
Solution 3 - WindowsOz EdriView Answer on Stackoverflow
Solution 4 - WindowsNeil NeymanView Answer on Stackoverflow
Solution 5 - WindowsKibbeeView Answer on Stackoverflow
Solution 6 - WindowsMichael RunyonView Answer on Stackoverflow
Solution 7 - WindowsMatthew SmithView Answer on Stackoverflow
Solution 8 - WindowsSteView Answer on Stackoverflow
Solution 9 - WindowsdevinmooreView Answer on Stackoverflow
Solution 10 - WindowsWo'O IdeafarmView Answer on Stackoverflow
Solution 11 - WindowsRoaldView Answer on Stackoverflow