5

I need to run batch scripts at shutdown of Windows 10 home edition, both on manual restarts and on automatic restarts due to Windows automatic updates. This needs to work while no users are logged in too. So far I've tried:

  1. Scheduled Task at eventid 1047 (discarded, won't wait till my script is over).
  2. Manually adding Group Policy shutdown scripts to registry and to C:\WINDOWS\SYSTEM32\GroupPolicy (discarded, won't run since it looks like the functionality is disabled by restraints imposed by the Windows edition.)
  3. Third party software. Weird enough, I couldn't find any free tool that would run as a service and allow me to add .bat files to be run at system (or service) shutdown.
  4. I tried AutoHotkey + NSSM [The Non-Sucking Service Manager] as a custom service, but AutoHotkey's events wouldn't trigger every time.

I'm quite disenchanted with this limitation. 🙁
Any ideas?

Edit1: Take into account Windows automatic updates.
Edit2: This needs to work while no users are logged in too.
Edit4: I'm waiting for the next Windows automatic restart due to a Windows update to see if running shutdown -a at the beginning of the batch file and then shutdown -s at the end within a scheduled task at shutdown event approach works.
Edit5: Bummer, it didn't work. The scheduled task did not start: Error: "Shutdown in progress". I guess my only hope is a third-party tool.

8
  • 6
    You can add shutdown -a to the beginning of your batch file then shutdown -s at the end. This will abort the shutdown and let your process execute then resume the shutdown when it's done.
    – Neelix
    Commented Apr 21, 2017 at 21:22
  • @Neelix, so far your proposed solution works with shutdowns triggered by shutdown -s but not by the Start menu's Power button. I'll have to wait till next Windows automatic update restart to see if it will work for me. I'll post the results back. Thanks.
    – B. Gray
    Commented Apr 23, 2017 at 2:03
  • What sort of stuff are you trying to do? It would help to know the context in which you are trying to execute something. Also, have you tried LOCAL group policy editor and putting in a test script that copies a file, in order to confirm that you are implementing the script correctly?
    – Jeter-work
    Commented Apr 27, 2017 at 16:40
  • I need to store a value in a .db file upon shutdown of the system. It is a monitoring program which needs this specific information to log stuff accurately. gpedit.msc is not present in Windows home edition. It seems this whole functionality is stripped off from the edition.
    – B. Gray
    Commented Apr 29, 2017 at 13:22
  • 2
    Does this answer your question? Run a script just before shutdown or reboot on Windows Home edition
    – Destroy666
    Commented Jun 29, 2023 at 1:09

3 Answers 3

2

I've done this with AutoHotkey before. This script is tested and working.

I made a basic batch file that will create a "test file" on the desktop. Just run the AHK file and leave it running. When the script detects a shutdown, it'll run the batch file.

Make sure you set the path to your text file at the top of the script.

AutoHotkey Script:

;==============================
;Set the path to your batch file.
path    := "C:\batch.bat"
;==============================

; Run script as admin. This will allow your batch file to be ran with admin priv.
if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
   ExitApp
}

; Only allow one instance of the script to run.
#SingleInstance, Force

; If you don't want an icon to show in the tray,
; remove the semicolon from the line below.
;#NoTrayIcon

; When script detects WM_QUERYENDSESSION (a shutdown), run OnShutDown function.
OnMessage(0x11, "OnShutDown")
return

OnShutDown(){
    ; Run the batch file.
    Run, % path
    ExitApp
}

Batch file I used for testing:

echo Write file before shutdown > %USERPROFILE%\Desktop\ShutdownTest.txt

If you wanted to, you could always execute your batch file commands directly through AHK.

3
  • I need this to work while no users are logged in too, because of Windows automatic updates. So this won't address such situation.
    – B. Gray
    Commented Apr 29, 2017 at 13:09
  • If no user is logged in, no AHK script can be running. If that's something you need, you'll have to find another solution other than this. Sorry. Commented Apr 30, 2017 at 4:39
  • OnMessage is not defined here I guess?
    – Zeeshan
    Commented Jan 23, 2018 at 5:27
1

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

Update: If it is something that needs to be done before the next user logs on, putting it in the machine startup script might fulfill your need. If it is something that has to be done before power off, shutdown script is your answer.

If Home edition does not allow you to modify the local policy to make these scripts work, then that is a limitation of the home edition and you may need to upgrade. However, TechNet does not narrow their explanation of how to set up a shutdown script beyond Windows #.

Also, the machine shutdown script will run with permissions of the computer. The user log off script will run with the logged on user's permissions.

Use a batch script to shut down? Just use the shutdown command ("shutdown -t 0") at the end of your shutdown script. Put a shortcut to the script on your desktop, use a power button icon. (Removed answer based on single user shutdown by script instead of start menu shutdown option).

2
  • I need the scripts to run on automatic restarts (due to Windows automatic updates), too. I've just clarified this on the question, I'm sorry.
    – B. Gray
    Commented Apr 24, 2017 at 22:37
  • From what I could gather, the whole functionality is stripped off from the Windows home edition.
    – B. Gray
    Commented Apr 29, 2017 at 13:26

You must log in to answer this question.

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