10

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.

3

4 Answers 4

17

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.

2
  • cannot find gpedit.msc on windows 10
    – Inspi
    Commented Jun 7, 2021 at 14:18
  • It needs to be enabled on Windows 10 Pro (control panel > programs and features > change windows components). For Windows 10 Home edition, it is not included by default, but there are lots of ways out there to install it, google is your friend.
    – Fazer87
    Commented Jul 7, 2021 at 9:18
2

This the path of registry

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

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)

option.

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 .
3
  • 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
    Commented Jun 26, 2014 at 9:58
  • That would be nice, sadly i have windows home edition, it doesnt come with a group policy editor.
    – user338847
    Commented Jun 26, 2014 at 11:48
  • check the updated answer for invoking group policy
    – Innovator
    Commented Jun 26, 2014 at 11:59
2

I've finally found out the solution after several months!

  • Open the Group Policy Management Console.
  • Then go to User Configuration\Policies\Windows Settings\Scripts (Logoff).
  • Now add your app or script.

Normally at this point is where it never worked for me.

  • The workaround is to go to User Configuration\Policies\Administrative Templates\System\Scripts.

  • And enable Display instructions in shutdown scripts as they run.

Run GPUPDATE /FORCE and enjoy! For shutdown you do the same steps but under Computer Configuration.

Group policies

0

First is first, thanks for pointing that with GPEDIT.MSC (manually installed on Windows Home) can be added Startup, shutdown, logon and logoff scripts.

But, how can that scripts (logon & logoff) be run with elevaled admin rigths?

I had also tried the Task Scheduler and logon works, but logoff do not work ... and i am desperated/frustated.

What i need is to run this command at Windows start:

C:\Windows\System32\reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v "ClearPageFileAtShutdown" /t REG_DWORD /d 1 /f

And what i also need is to run this other command at Windows reboot/shutdown:

C:\Windows\System32\reg.exe ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v "ClearPageFileAtShutdown" /t REG_DWORD /d 0 /f

But at a point that takes effect; please note if i manually run the command (in task scheduler) to set that value to 0, if i run it just one second before click shutdown/reboot it works, no pagefile cleared (take in mind 1GiB SSD is Veracrypt full system encrypted).

Let me explain why i am so interested:

  • One needed application runs at user logon (in admin rule) and if sees that registry key is not 1 (it checks it periodically every 10 or 20 seconds), locks the computer and do not allow you to use it, it runs in a mode it can not be killed (ProcessExplorer does not even kill it in admin rule).
  • That application also ensures that virtual ram is configured as fixed with twice the RAM installed, if not it automatically changes it and reboots Windows.
  • I do not want (neither need) 512GiB of virtual ram to be filled with zeroes at shutdown/reboot every time, think of a 1GiB SSD with Windows on a VeraCrypt cyphered system partition, that will kill my 1GiB SSD live very fast, since SSD does not recieve zeroes (recives such zeroes encrypted, so they are no zeroes anymone), that is writting half of the SSD on every reboot/shutdown.

In short words:

  • I need to enable ClearPageFileAtShutdown at Windows startup, but disable it as soon as shutdown/reboot starts; to avoid SSD live shortened very fast.

Note: The computer has 256GiB of pysical RAM, i can not put 512GiB becuase of such application, it is so weird that requieres a 1GiB max size system drive or refuses to let you use Windows (sure you noted, i hate that app but i am forced to use it and they do not pay me for SSD every week or so), it also forces a reboot every 45 minutes, so makeing counts, 0.5GiB every 45' = 16 TiB per day (480 TiB per month) only just because clearing the pagefile... i hate it... not to mention time to shutdown/reboot increase a lot (if every reboot/shutdown is done at max time that app allows, aka 45', of every hour a total of 16' is dedicated to clearing pagefile, at 24h that is >6h on that clearing pagefile per day if you use it 45' every time; 550MiB/s speed), now i think you can understand my interest of avoid such clearing (mostly the SSD live but also time).

EDIT1: I had seen that Task Scheduler command is run (at shutdown/reboot) and registry key is set to 0, but at that shutdown/reboot it takes no effect, the change is done too late; so it must be a question to find an event that triggers sooner, since (User32 1074 and/or WinLogon 7002) does not trigger soon enough, user logout neither, computer lock neither; all of them are triggered too late.

EDIT2: Neither it worked soon enough with personal filter like SCHTASKS /Create /SC ONEVENT /mo "Event[System[(EventID=1074)]]" /EC Security /tn on_shutdown_normal /tr "c:\some.bat"

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 4, 2022 at 13:00
  • It seems to me your answer had better be a new question. Commented Mar 4, 2022 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .