43

Is there any way, in Windows 7 Professional, to run a batch script (e.g., a .BAT file) when the user clicks on "shutdown" (not a batch file scheduled to shut down the machine, just one that runs only if the user clicks on "shutdown")?

Ideally, such a script would show the command prompt window and offer an option to cancel the shutdown procedure.

Solutions that do not require a third-party utility would be preferable.

bahrep
  • 29,961
  • 12
  • 103
  • 150
PNS
  • 19,295
  • 32
  • 96
  • 143
  • Did you manage to prevent the shutdown? If yes, how did you do it? Thanks – Alex Jul 02 '13 at 11:38
  • 1
    If you want to prevent users from shutting down the computer, you can disable access to it in the start menu through Group Policy. Go to _User Configuration\Policies\Administrative Templates\Start Menu and Taskbar_ and look for **Remove and prevent access to the Shut Down, Restart, Sleep, and Hibernate commands**. This will hide those options from the start menu. If you need to allow them to restart, you can add a batch file to the _All Users\Desktop_ folder to call the command `shutdown.exe \r \t 10` which will force a restart of the computer after 10 seconds. – Slicktrick Feb 07 '17 at 16:14
  • OK, how do you debug the scripts then? Even just dumping the output to a file would help. – RoboJ1M Jul 04 '18 at 09:20
  • Related question https://stackoverflow.com/q/52581235/45375 asks how to _script_ (automate) the installation of a shutdown script (without the added requirement of presenting a visible console window to the user). – mklement0 Oct 01 '18 at 18:06

6 Answers6

70

You can create a local computer policy on Windows. See the TechNet at https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dn789197(v=ws.11)

  1. Run gpedit.msc to open the Group Policy Editor,
  2. Navigate to Computer Configuration | Windows Settings | Scripts (Startup/Shutdown).

enter image description here

bahrep
  • 29,961
  • 12
  • 103
  • 150
  • Is there any way to prevent the shutdown from within a script attached to tbis Shutdown event? I have tried a batch file with shutdown /a but it doesn't seem to work. – Alex Jul 01 '13 at 21:46
  • 4
    thanks. Note you will need `Win Professional` or `Ultimate` to do this. If you have standard home version (like I did), then you can manually add the group policy editor from [here](http://www.askvg.com/how-to-enable-group-policy-editor-gpedit-msc-in-windows-7-home-premium-home-basic-and-starter-editions/). – benscabbia Dec 01 '15 at 21:07
  • 5
    Very helpful in scripting a `savestate` for a VirtualBox VM before shutdown. Thanks! – jbwebtech Jan 09 '17 at 00:40
  • 2
    How would I be able to do this on "sleep" instead of "shutdown" ? – doubleOrt Jan 21 '18 at 18:37
  • 2
    Note that, as mentioned [in this question](https://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows), if you want a batch script to run at Logoff, I found referencing the .bat file directly didn't work. However, if I used `C:\Windows\System32\cmd.exe` as the Script Name and `/C C:\path\to\batch\script.bat` as the Script Parameters, it did – Nova Oct 08 '20 at 07:19
5

Well, its an easy way of doing some registry changes: I tried this on 2008 r2 and 2016 servers.

Things need to be done:

  1. Create a text file "regedit.txt"
  2. Paste the following code in it:
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\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"="terminate_script.bat"    
"Parameters"=""    
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00    

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\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\Scripts\Shutdown\0\0]    
"Script"="terminate_script.bat"    
"Parameters"=""
"IsPowershell"=dword:00000000
"ExecTime"=hex(b):00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
  1. Save this file as regedit.reg extension

  2. Run it on any command line using below command:

    regedit.exe /s regedit.reg
    
mklement0
  • 382,024
  • 64
  • 607
  • 775
asif
  • 69
  • 1
  • 3
4

Create your own shutdown script - called Myshutdown.bat - and do whatever you were going to do in your script and then at the end of it call shutdown /a. Then execute your bat file instead of the normal shutdown.

(See http://www.w7forums.com/threads/run-batch-file-on-shutdown.11860/ for more info.)

rss2363
  • 77
  • 2
3

I found this topic while searching for run script for startup and shutdown Windows 10. Those answers above didn't work. For me on Windows 10 it worked when I put scripts to task scheduler. How to do this: press Windows key and write Task scheduler, open it, then on the right is Add task... button. Here you can add the scripts. PS: I found the actions for startup and logout user, there is none for shutdown.

s.dragos
  • 582
  • 2
  • 8
  • 24
paka
  • 434
  • 1
  • 4
  • 9
  • 1
    I can not find the `logout user` option when doing this. Is it called something else or how do I find it? – Kajsa Apr 04 '18 at 15:59
3

Programatically this can be achieved with SCHTASKS:

SCHTASKS /Create /SC ONEVENT /mo "Event[System[(EventID=1074)]]" /EC Security /tn on_shutdown_normal /tr "c:\some.bat" 

SCHTASKS /Create /SC ONEVENT /mo "Event[System[(EventID=6006)]]" /EC Security /tn on_shutdown_6006 /tr "c:\some.bat" 

SCHTASKS /Create /SC ONEVENT /mo "Event[System[(EventID=6008)]]" /EC Security /tn on_shutdown_6008 /tr "c:\some.bat" 
npocmaka
  • 55,367
  • 18
  • 148
  • 187
2

For the above code to function; you need to make sure the following directories exist (mine didn't). Just add the following to a bat and run it:

mkdir C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup
mkdir C:\Windows\System32\GroupPolicy\Machine\Scripts\Shutdown
mkdir C:\Windows\System32\GroupPolicy\User\Scripts\Startup
mkdir C:\Windows\System32\GroupPolicy\User\Scripts\Shutdown

It's just that GP needs those directories to exist for:

Group Policy\Local Computer Policy\Windows Settings\Scripts (Startup/Shutdown)

to function properly.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Dugb
  • 21
  • 3