How to schedule a windows task to be run at logoff

Dominique picture Dominique · Dec 1, 2015 · Viewed 9.3k times · Source

Good morning,
I'm working with a Windows 7 computer.
I would like my computer to warn me about something I may not forget, at every time I log off from my system.

In order to do this, I am thinking of the "Windows Task Scheduler".
However, in the list of possible triggers (the "Begin the task" combobox), I only see the following possibilities:

  • On a schedule (fix date/time)
  • At log on (while logging into the PC)
  • At startup (while starting the PC)
  • On idle (when the PC is not occupied with other tasks, I presume)

I don't see the possibility "At log off" or "At shutdown".

Does anybody know if this possibility can be added (maybe by handling the registry)?

Thanks
Dominique

Answer

Syberdoor picture Syberdoor · Dec 2, 2015

As far as I have researched there is a simple reason why this is not supported. It can not be guaranteed that the task scheduler service is running at the point the logoff event is fired, and if it were it could not be guaranteed that the script you start with it would not be terminated immediately by the shut down of your computer.

You can bypass this by adding a trigger "on an event" and using one of the event ids of the shutdown but for the reasons stated above, it might not be the best idea.

I would rather recommend using a shutdown/logoff script defined per policy. This is the official method for such things and it is guranteed to execute completely before a shutdown/logoff. If you got a Windows version with GPO support you can just start gpedit.msc and Go to Computer Configuration\Windows Settings\Scripts for Shutdown and User Configuration\Windows Settings\Scripts for logoff scripts and create yours. A detailed article explaining those scripts can be found here.

If your OS version does not support GPOs you should be able to recreate the necessary registry entries. I did not try this myself but several articles seem to suggest that what you'd need is:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group
Policy\State\Machine\Scripts]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group
Policy\State\Machine\Scripts\Shutdown]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group
Policy\State\Machine\Scripts\Shutdown\0]
"GPO-ID"="LocalGPO"
"SOM-ID"="Local"
"FileSysPath"="C:\\WINDOWS\\System32\\GroupPolicy\\Machine"
"DisplayName"="Local Group Policy"
"GPOName"="Local Group Policy"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group
Policy\State\Machine\Scripts\Shutdown\0\0]
"Script"="C:\\bin\\shutdownscript.cmd"
"Parameters"=""
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00

put into a .reg file. The only line you'd need to to change is: "Script"="C:\\bin\\shutdownscript.cmd"