We help IT Professionals succeed at work.

running a shutdown script before Windows 7 OS shutdown

johnnyt29 asked
Last Modified: 2012-05-21
How do I run a script at shutdown BEFORE Windows 7 shuts down running applications?

I know about the Group Policy way to run a shutdown scripts but that script runs AFTER Windows has sent the kill command (or whatever) to the various running apps. I need something that runs before that command is issued.

I have a Home Automation app  (HomeSeer) that does not clean up when Windows tells it to shutdown - a process that takes about 20 secs when one manually tells the app to exit - and that has caused database corruption on a few occasions. Yes, the app developer should have taken care of this but they've chosen not to for some reason (support for the software has gone downhill so they aren't likely do anything about it now)
Watch Question

This problem has been solved!
(Unlock this solution with a 7-day Free Trial)


Anything else, ideally built into Windows? Group policy way does not work as it runs script AFTER shutting down apps.

Windows has a time set to saving settings and exit upon shutdown you could modify the registry to tell windows to wait a few secs longer till programs have shut down fully before shutting down that way allowing your (HomeSeer) to cleanup first?
This will show you how to set the Wait to Kill time Windows 7 waits for programs to save data and close before shutting down
Look to option two > Manually set


Thanks for the suggestion but it doesn't work. I think the "wait to kill" time is if the program does not respond to whatever the app shutdown command is. In this case the app responds within half a second and simply shuts down hard (closes window without cleaning up first). It wasn't properly written IMO. what I need to is 1) run a VB script that makes an API call to a HomeSeer function called shutdown and 2) delay having the "shutdown" command sent to the app while the script runs (~15-20 secs). The VB script is what I run when I'm the one that initiates the shutdown. I preceded it by the "shutdown -r /t 30" command, which buys me the 30 sec delay I want before the shutdown command is actually sent to any apps.

Since this is designed to automate things in your home why are you turning the system off?
From what I sourced you run this on several mobile devices as well,
Home Control with Smart Phones & Tablets
Home Automation app  (HomeSeer)

Please explain>that does not clean up when Windows tells it to shutdown, in my humble opinion it should never be shutdown.
Could you provide model and make?
And what service this device performs so I can better understand it's source.
HomeSeer Control System Manuals & Guides


Am running the HomeSeer 2 software (http://store.homeseer.com/store/HomeSeer-HS2-Home-Automation-Software-Download-P103C5.aspx) in a Windows 7 32 bit virtual machine on a VMWare ESXi 5.0 server. I'm not personally connecting to it via mobile devices but yes, it's possible.

The VM is generally on all the time but I do have to reboot it once in a while for various reasons. Doing it manually or via a HomeSeer event is not a problem though; I have my VB script for that. The two scenarios I am trying to get working are 1) when the UPS kicks in because of a power outage it runs a script that tells my ESXi host to shutdown, which then sends "shutdown guest" message to all my running VM's. The second one is when I'm telling the ESXi host to restart for whatever reason. I don't want to have to go to that VM to shut it down before telling my host to shutdown. While these scenarios don't happen often they do happen and I'd like to deal with them if I can.

Thankyou so much that clarifies a lot more, hopefully the experts in VB will now know what to offer you, I on the other hand don't have the knowledge on scripting in this area
what I can offer
is sourced for your consideration,
 first one is from EE may not be relevant to your situation however

How to automatically shut down VMware ESXi gracefully during power failure using an APC UPS.

How to automatically start and shut down VirtualBox machines?


Just to clarify, I'm not having any problem with VMWare - it does everything it's supposed to do as expected when it's being told to shutdown, either by me or the UPS.
What is recommend is have a batch file call the program then in the last line is the command to clean up the database.and put a shortcut on the desktop for the customer to run the program with.

Example to batch file

@echo off
call "" "C:\Program Files (x86)\program where it is\program.exe"
call "" "C:\Program Files (x86)\program where it is\program.exe" /clean up database

Open in new window

if you need a window type batch file then i recommend something like this:




I already have a batch file, which I use when I am manually shutting down or restarting the VM. What I need is to intercept or pre-empt the Windows shutdown messages that may be coming from other (automated) sources, such as my VM host telling all VM's to shut down.

It looks like the only available solution for this may be the first one provided - from Ackles - but it's more involved that I want. I'll give this a few more days but I think the points will have to go to Ackles on the basis that it looks like it would work (although I didn't test it)

Why don't you give it a shot?
Maybe you will save yourself lot of hassle & if there is something which is not appropriate we all can assist you in reaching what you want.

I prefer the way of GP because you have more control & you it's more granular.

All the best,


didn't try this but it appears to be the only way to intercept a windows shutdown and be able to call a batch file programmatically BEFORE windows sends the shutdown command to my app. The alternatives tell my app to shutdown first then runs the batch file.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions