Determine whether a shutdown is pending

Solution 1:

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

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • Errorlevel - Almost all applications and utilities will set an exit code when they complete/terminate.
  • if - Conditionally perform a command.
  • shutdown - Shutdown the computer.

Solution 2:

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.