Executing a batch script on Windows shutdown

133,219

Solution 1

You can create a local computer policy on Windows. See the TechNet at http://technet.microsoft.com/en-us/magazine/dd630947

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

enter image description here

Solution 2

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
    

Solution 3

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.)

Solution 4

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" 

Solution 5

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

Share:
133,219
PNS
Author by

PNS

Updated on November 12, 2020

Comments

  • PNS
    PNS over 3 years

    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.

    • Alex
      Alex almost 11 years
      Did you manage to prevent the shutdown? If yes, how did you do it? Thanks
    • Slicktrick
      Slicktrick over 7 years
      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.
    • RoboJ1M
      RoboJ1M almost 6 years
      OK, how do you debug the scripts then? Even just dumping the output to a file would help.
    • mklement0
      mklement0 over 5 years
      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).
  • Alex
    Alex almost 11 years
    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.
  • benscabbia
    benscabbia over 8 years
    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.
  • jbwebtech
    jbwebtech over 7 years
    Very helpful in scripting a savestate for a VirtualBox VM before shutdown. Thanks!
  • doubleOrt
    doubleOrt over 6 years
    How would I be able to do this on "sleep" instead of "shutdown" ?
  • Kajsa
    Kajsa about 6 years
    I can not find the logout user option when doing this. Is it called something else or how do I find it?
  • Nova
    Nova over 3 years
    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 did