Determine whether a shutdown is pending

8

1

Windows has the command shutdown /t to shutdown or restart the computer after a certain delay. A pending shutdown can be aborted with shutdown /a.

But how can I determine whether and when a shutdown is currently pending or scheduled, without aborting it?

ygoe

Posted 2016-01-19T10:39:33.890

Reputation: 1 597

Shutdown.exe will record events in the Windows SYSTEM Event log with a Source=User32. When a Shutdown command is issued with a timeout of > 120 seconds, event ID 1074 is logged. – DavidPostill – 2016-01-19T11:02:17.403

I can find this message in the event log. But it doesn't say when the restart is scheduled for. It just tells the user and reason. – ygoe – 2016-01-19T11:06:41.013

You need to know the time of the pending shutdown? Please add this important information to your question (you have just invalidated the answer I was writing :/) – DavidPostill – 2016-01-19T11:08:22.363

Anything useful from the -d option? Shutdown. Maybe you can find useful the Shutdown Event Tracker.

– Hastur – 2016-01-19T11:12:14.690

I have added the information to the question. /d specifies the shutdown reason, which I did. This information was recorded in the event log. But not the timeout from /t. Shutdown Event Tracker does not seem to be useful. It does not show the timeout, and it needs to be set up in advance. – ygoe – 2016-01-19T11:17:35.227

Can't help with the time, but from a related Q+A on SO you can detect whether a shutdown is pending... the safest is to run shutdown /a which will respond Unable to abort the system shutdown because no shutdown was in progress.(1116) and set a non-zero ERRORLEVEL if no shutdown is scheduled (or nothing and zero ERRORLEVEL if one was).

– TripeHound – 2016-01-19T11:50:14.850

Just dumped registry on a Windows 7 box before and after shutdown /r /t 10000 and there's precious little difference: a shuffling in Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\*, some bytes tweaked in Software\Microsoft\Windows\CurrentVersion\Explorer\UserAssist\{CEBFF5CD-ACE2-4F4F-9178-9926F41749EA}\Count). – TripeHound – 2016-01-19T12:01:35.020

@TripeHound `/a' will abort an existing shutdown. The OP doesn't want to do that. – DavidPostill – 2016-01-19T12:29:56.003

@DavidPostill As noted on the linked question, you can also use shutdown /r /t _large-number_ which will give an error if a shutdown is already pending (and if it wasn't, gives you time to cancel it). – TripeHound – 2016-01-19T12:42:51.167

@TripeHound I know. See my answer ;) – DavidPostill – 2016-01-19T12:56:25.683

Answers

9

How can I determine whether and when a shutdown is currently pending or scheduled, without aborting it?

I don't think it is possible to determine when the shutdown will happen.

You can determine if a shutdown is scheduled using the following algorithm:

  1. Run a "test" shutdown using shutdown /t xxx with a large value for the time.

    • For Windows 7 or later the maximum time allowed was increased from 600 seconds to 315,360,000 seconds (10 years)
  2. If there is already a shutdown pending then shutdown /t xxx will fail with errorlevel 1190:

    A system shutdown has already been scheduled.(1190)

  3. If you don't get the above error then you know there was no previous shutdown scheduled, so you need to delete the "test" shutdown using shutdown /a.

The above can be done in a batch file:

@echo off
rem perform a "test" shutdown with a large time
shutdown /t 999999
rem if there is already a shutdown pending then %ERRORLEVEL% will be 1190
if %ERRORLEVEL% equ 1190 (
  echo A shutdown is pending
  ) else (
  rem cancel the "test" shutdown
  shutdown /a
  echo No shutdown is pending
  )

Note:

  • I haven't tested the above batch file as I don't wish to shutdown my PC at this time.

Further Reading

DavidPostill

Posted 2016-01-19T10:39:33.890

Reputation: 118 938

It instills a great deal of confidence in your code that you haven't dared to run it yourself, even though it is supposed to leave the computer running. – TamaMcGlinn – 2020-01-21T08:51:48.247

@TamaMcGlinn I've tested it since I wrote the answer ... – DavidPostill – 2020-01-21T08:53:27.243

0

A rather more complicated way of finding out whether or not a shutdown is scheduled is to debug winlogon.exe and check the status of the ShutdownInProgress flag. You'll need debugging tools for Windows.

I haven't tried it but this MSDN blog post explains what happens behind the scenes when Windows shuts down and how to debug winlogon.exe (which is a kernel process). The debugger command to get the status of the flag seems to be:

dd winlogon!ShutdownInProgress l 1
01062b3c  00000000

If you know how to debug kernel processes in Windows, you could try it out. This beginner's guide to debugging with CDB and NTSD might help.

Vinayak

Posted 2016-01-19T10:39:33.890

Reputation: 9 310

Uhm... Interesting. :-) That will tell me as much as the error message from a probing shutdown /t 99999, right? – ygoe – 2016-01-19T16:36:36.957

2@LonelyPixel Right. This method is only for the masochists :) – Vinayak – 2016-01-19T16:38:16.393