I need to run a script when the computer shuts down or restarts to remove some files. I know there is a registry key for autorun.. but I couldn't find something for shutdown/reboot.

[update] I want to do this on Windows 7 Home edition, so I don't have the group policy editor.


The easiest way to do this is with a local group policy.

To do this, go to your local group policy editor (Click Start, type gpedit.msc in the Start Search box, and then press ENTER.)

Once this is loaded, go to the following path: Computer Configuration > Windows Settings > Scripts (Startup/Shutdown) > Shutdown (See Below)

enter image description here

Within here, you can choose to select scripts which will run on your machine as part of startup/shutdown. You can also follow the User Config > Windows Settings > Scripts (Logon/Logoff) to setup scripts which run at logon and logoff.

The differences are:

Startup Runs before a user logs on. Startup scripts execute as the local system account

Logon Runs as soon as a user has entered their password. This executes as the user who is logging on.

Logoff Runs as the users session is logging off (whether shutting down or logging off to change user) and executes as the user logging off

Shutdown Runs after logoff and only as part of the machine shutting down. This runs as the local system account.

Hope this helps.

| improve this answer | |

This the path of registry


or alternatively

Launch the Group Policy Editor by clicking Start, then type gpedit.msc and hit Enter. Now navigate to the

User Configuration > Windows Settings > Scripts(Log on/Log off)


Add your script there.

Check this for home edition:

To open the Local Group Policy Editor as an MMC snap-in

Open MMC. (Click Start , click in the Start Search box, type mmc , and then press ENTER .)
On the File menu, click Add/Remove Snap-in .
In the Add or Remove Snap-ins dialog box, click Group Policy Object Editor , and then click Add .
In the Select Group Policy Object dialog box, click Browse .
Click This computer to edit the Local Group Policy object, or click Users to edit Administrator, Non-Administrator, or per-user Local Group Policy objects.
Click Finish .
| improve this answer | |
  • This registry key will only run when the computer starts up, not during shutdown. A file may then be in use already. The GPO option is the only viable solution, because these scripts are started AFTER the user is logged out, so any file that was previously locked no longer is. I used this to make a copy of my profile which is the only way it will work for non-domain users. – LPChip Jun 26 '14 at 9:58
  • That would be nice, sadly i have windows home edition, it doesnt come with a group policy editor. – user338847 Jun 26 '14 at 11:48
  • check the updated answer for invoking group policy – Innovator Jun 26 '14 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.