2

I have been trying to create a batch file(s) that triggers my Windows 8 PC to begin shutting down after 30 seconds has elapsed and that also has a warning message: "Your PC will shutdown in 30 seconds! Please press Cancel button to abort Shutdown." I have created the following batch files on my desktop:

shutdown /s /c "Your PC will shutdown in 30 seconds! /t 30

and:

shutdown /a

and it does shutdown the pc, but the message will not show until exactly 30 seconds later and goes away within 1 second and starts shutting down with no way to stop it. I have even tried to do the shutdown /a batch file before the 30 seconds is up with no luck in stopping the shutdown.

Is the commands not correct for Windows 8?

I would like to have one script with the popup message warning and with a cancel (abort) button prior to the 30 seconds expiring or at least get the 2 batch files to work in Windows 8 with a warning message and the abort batch file both working.

Not sure if Windows 8 has a command or something that works but would like to get it working. Any help is so greatly appreciated!!!

Thanks you very much!

Billy

Billy
  • 343
  • 2
  • 4
  • 7

5 Answers5

1

This is somewhat easier and has been kitchen tested in Windows 8.

:: Shutdown.bat
:: Messages a forced shutdown then waits for a Ctrl+C to abort,
:: any key to execute immediately, or shuts all programs and  
:: the computer down after Timeout /t (n) seconds automatically.
:: 
@echo off
echo.
echo.
:: 
echo Press Ctrl+C to Abort imminent Shutdown because I'm
timeout /t 120
::
shutdown.exe /s /t 5
exit
Steve987
  • 11
  • 1
  • If you are using Task Scheduler to call the shutdown at a specific time, call it like this, in order for the Command console to display: :: calls shutdown.bat with a cmd /k to show the console window :: even if called by the Task Scheduler :: cmd /k shutdown.bat exit – Steve987 Aug 13 '13 at 22:14
0

Try

@echo off
echo Your PC will shutdown in 30 seconds! Press CTRL+C to abort.
ping -n 31 127.0.0.1>nul
shutdown /s

The PING command is a secure way of pausing the batch script for desired amount of time. More info here: How to wait in a batch script?

Pressing Ctrl-C will abort a batch script from continuing.

Community
  • 1
  • 1
jtheman
  • 7,421
  • 3
  • 28
  • 39
  • jtheman, Thanks for your quick reply! Your answer works by bringing up a cmd prompt immediately and the CTRL+C works. I was not expecting a cmd prompt (black box) type of alert, but it does work. Any other way besides a cmd prompt? I saved your code as a .bat file. Is it possible to have it abort with just a single key like the A or C key or a funtion key like F12? I cannot thank you enough for your help and look forward to your reply! – Billy Mar 07 '13 at 08:32
  • Well it is more complicated than you think to make it work - IF you want the countdown to proceed and allow for a custom keypress to abort during the timeout. Ctrl-C as a general abort feature is otherwise quite simple. But otherwise see this post: http://stackoverflow.com/questions/12331428/how-to-ask-for-batch-file-user-input-with-a-timeout (Gaurav's example below won't work as the abort keypress A either must happen BEFORE or AFTER the 30s delay - not DURING the delay) – jtheman Mar 07 '13 at 19:54
  • jtheman, I agree with you. It more complicated after viewing the post. I will just use your working batch using the CTRL+C option. I want to thank you very much for your help!!!! – Billy Mar 08 '13 at 03:32
  • jtheman, Update-Clicking on the .bat file works fine however when I add it to my backup program 'when finished' setting (there is a "post finish backup-command" setting to run a .bat file) it's not showing the cmd prompt message & abort instructions. When the backup finishes, it waits the 30 seconds with no cmd prompt at all & the PC shuts down immediately. Strange that directly clicking on the .bat file works as supposed too, but when the backup finishes and initiates the .bat file, it will shutdown the PC after 30 seconds but with no cmd prompt with message & abort option. Any ideas? Thanks! – Billy Mar 09 '13 at 08:18
  • I don't know. It must be something how your backup software executes the script. Normally batch scripts always run in console windows. So I can't help you here... – jtheman Mar 10 '13 at 23:04
0

Hi you can try this,

@echo off
echo "Your System will shutdown in 30 seconds. To abort press A"
SET Option=Choice:
if "%Option%"=="A" GOTO Sub_MenuA
shutdown /s
:Sub_MenuA
exit 0
  • Gaurav Kolarkar, Thanks for your quick reply! Your answer works by bringing up a cmd prompt (black box) immediately with the message, but pressing A does to work and it does not wait for 30 seconds. It starts the shutdown within seconds. Any ideas to fix the problem? Your help is greatly appreciated and I look forward to your reply! Thanks! – Billy Mar 07 '13 at 08:41
0

Hi try this small clever code which i created and have used task scheduler which is fun to use.

title GO HOME
@echo off
:MAIN
echo.
echo.
echo.
echo Do you wanna shut down?
echo.
echo The time is %time%
echo.
echo.
echo.
echo.
set /p input= y or n?     
if %input%==n goto work
if %input%==y call bye.bat

:work
echo.
echo.
echo.
echo. Alright, keep working busy bee.

TIMEOUT 5

Create one more bat file and save as bye.bat and copy paste this code:

echo Have a nice evening, Cya.

shutdown.exe /s /t 0
0

Here is another option. This BAT file asks the user a YES/NO question to determine if the shutdown should continue. No need for the user to press CTRL+C to abort. The Shutdown will not continue until the user enters a choice.

@ECHO OFF
:: This batch file allows the user to choose to shutdown immediately, or cancel and   return to Windows
::
CD\
CLS
ECHO.
:LOOP
ECHO Would you like to shutdown immediately?
ECHO.
ECHO   Y = SHUTDOWN IMMEDIATELY (WARNING!! Unsaved work will be lost)
ECHO   N = CANCEL and return to Windows
ECHO.
SET Choice=
SET /P Choice=TYPE YOUR CHOICE AND PRESS ENTER: 
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF '%Choice%'=='y' GOTO ShutdownNow
IF '%Choice%'=='n' GOTO Leave
ECHO "%Choice%" is not valid. Please try again.
ECHO.
GOTO Loop
:ShutdownNow
ECHO.
shutdown /s /t 1

:Leave
Exit
pmn02
  • 1