Wednesday, February 01, 2012

How to run a script on logout/shutdown in windows 7

I have several mounted (windows share) network drives on my Dell laptop. I need them for my daily work. However, at the end of the day I want to close them and clear up some other stuffs. I can use Group Policy(GP) editor snap-in to assign logoff scripts as described here and a screenshot shown below:



But, GP editor may not be availble on all versions of Windows 7. The alternative, I figured out, is to use Task scheduler that comes (probably?) with all flavors of Windows 7. Task scheduler in Windows 7 is really nice, but, I did not find a way to invoke a script when I logout from my computer or shut it down at the end of day's work. The workaround is to create a task that triggers "when some event is logged". To use this alternative, follow these steps:

1. Start task scheduler
2. Enter General properties:

3.  Go to Trigger tab and event details as follows:
4. Enter action details:


5. Rest of the settings can be left default or changed as per needs
6. Save the task and test the script is invoked by manually restarting the computer.


That is all that needed to run a script on logout (shutdown actually) from task scheduler. 

Note: this way script is fired only at shutdown actually. I did not find a easy way to trigger logout security events. I tried with event IDs 4634 and 4647, both did not work as expected.