How to force restart a Windows box using VBScript?

tloach picture tloach · Oct 9, 2008 · Viewed 47.4k times · Source

I'm trying to find a way to force Windows to reboot, and I am running into issues. I've tried

Set OpSysSet = GetObject("winmgmts:{authenticationlevel=Pkt," _
     & "(Shutdown)}").ExecQuery("select * from Win32_OperatingSystem where "_
     & "Primary=true")
for each OpSys in OpSysSet
    retVal = OpSys.Reboot()
next

I've also tried using the shutdown -f -r command, and in both cases I sometimes get no response, and if I try again I get an error saying "Action could not complete because the system is shutting down" even though no matter how long I leave it it doesn't shut down, it still allows me to start new programs, and doing a shutdown -a gives the same error. How can a script be used to force Windows to reboot?

Answer

Matt Hanson picture Matt Hanson · Oct 9, 2008

Try replacing:

retVal = OpSys.Reboot()

With:

retVal = OpSys.Win32Shutdown(6)