Windows – Run a script just before shutdown or reboot on Windows Home edition

windows 7

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.

Best Answer

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.