I know that there are threads about this, I found this one: How to schedule a task to run when shutting down windows but it fails for me.
I wrote a little program in LabView and made an exe out of it. It runs perfect when I double click the exe in normal windows operation (it takes just a few seconds to complete). Then I put the exe in a batch file and that batch file in the group policy as a shutdown script. The result is that the computer hangs when shuting down. It shows the shutdown screen for minutes and I have to power cycle the PC to start it again. I even tried only a simple file copy command in the batch file with the same result. What am I doing wrong? Any hints are greatly welcome
Thanks a lot
Martin
thank you for your answer, it made me try some more things and now I was successful. I want to give some results here, maybe they help someone else:
- Additional to the group policy where you specify the exe that shall run before shutdown there is another usefull setting that can be made in gpedit (I have a german Win 7 so maybe the names are not exactly what you have): computer configuration - adminstrative templates - system - scripts: there you find "run scripts at shutdown visible". If this is not activated you will not see what the computer does during shutdown. Not activated is the default
- in gpedit under computer configuration - windows settings - scripts: shutdown you can insert as the script name a *.exe or if you want to run a batchfile use cmd.exe and enter as parameter /C "full path to batch\batchname.bat" (you need the /C and the quotes !!). In the thread mentioned in my question Dan Stevens says, giving the batchname.bat as the script name directly does not work. I didn't try this.
It's really nice to see my small program run just before shutdown
Thanks again for your help
Martin
I had to enable another option to make my AutoHotKey script (which i compiled into an .exe) before shutdown. The script would open an Excel file and add some values. In order to make it work I also had to enable the "Specify maximum wait time for Group Policy Scripts".
Just to make it more clear all the steps I had to do in Windows 10 set to UK English:
- Open Edit Group policy, by typing it into the Windows start menu for example.
- Go to Local Group Policy > Windows Setings > Scripts (Startup/Shutdown) > Shutdown
- Add your .exe here.
- Close window by clicking on OK.
- Go to Local Group Policy > Adminstrative Templates > System > Scripts
- Enable Display instructions in shutdown as they run.
- Enable Specify maximum wait time for Group Policy scripts. I set mine to 5000 ms.
Whenever I shutdown I first see the "Shutting down" and then the screen becomes black, it runs the script (opening Excel), closes Excel and then really shuts down.
来源:https://stackoverflow.com/questions/28234346/running-a-program-before-shutdown-in-win7-and-win-xp