Executing a batch script on Windows shutdown

前端 未结 6 600
旧巷少年郎
旧巷少年郎 2020-12-08 06:55

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

相关标签:
6条回答
  • 2020-12-08 07:36

    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.

    0 讨论(0)
  • 2020-12-08 07:36

    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.

    0 讨论(0)
  • 2020-12-08 07:41

    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
      
    0 讨论(0)
  • 2020-12-08 07:43

    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" 
    
    0 讨论(0)
  • 2020-12-08 07:44

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

    0 讨论(0)
  • 2020-12-08 07:47

    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

    0 讨论(0)
提交回复
热议问题