Executing a batch script on Windows shutdown
Asked Answered
L

6

44

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.

Lumbar answered 15/9, 2012 at 5:45 Comment(4)
Did you manage to prevent the shutdown? If yes, how did you do it? ThanksIdiolect
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.Overanxious
OK, how do you debug the scripts then? Even just dumping the output to a file would help.Underdrawers
Related question 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).Guttural
B
71

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

Bethany answered 15/9, 2012 at 5:56 Comment(5)
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.Idiolect
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.Brabble
Very helpful in scripting a savestate for a VirtualBox VM before shutdown. Thanks!Strasbourg
How would I be able to do this on "sleep" instead of "shutdown" ?Feminine
Note that, as mentioned in this question, 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 didDigital
J
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
    
Jolandajolanta answered 8/12, 2017 at 6:52 Comment(0)
S
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.)

Stinkpot answered 27/6, 2014 at 15:43 Comment(0)
G
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.

Gyromagnetic answered 1/3, 2018 at 10:1 Comment(1)
I can not find the logout user option when doing this. Is it called something else or how do I find it?Anodyne
I
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.

Intent answered 13/3, 2018 at 2:42 Comment(0)
P
2

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" 
Pass answered 11/11, 2020 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.