Search code examples
user-interfacebatch-filepopupshutdown

Shutdown batch command with time pop-up


I'm looking for a batch file code that shuts down a computer after a certain amount of time. I've got the obvious answer, which is:

shutdown -s -f -t 120

but say I want to shut down not after two minutes, but five, or ten?

Is there a way to make a .bat file that shuts the computer down after querying the user (via pop-up or otherwise) for how many minutes until the shutdown.

Now, I'm not very techy myself, but here's how I imagine this: A user runs the .bat file, and is presented with a pop-up window asking for how long until the shutdown. The user types '5', and presses 'enter'. The batch file then force-closes all open programs, and shuts down after 300 seconds (5 mins).

Additionally, can the batch file have some form of timer built in?

I've seen it done, but the cmd window is large and bulky.
(Optional: somehow incorporate an abort shutdown feature into the same batch file.)
[I know I can use shutdown -a on a separate batch file, but that's just no fun.]

Edit: Similar to the following code, except it shuts down the computer once the timer is up. Also, with a much smaller cmd box, if possible?

@ECHO off
cls
color 0B
:taskkill
echo.
echo.
echo.
echo.
echo.               
echo.               
echo.               
echo.               
echo.               
echo.
echo                                Program To Shutdown
echo.    
set /p taskkill=                      
if "%taskkill%" == "%taskkill%" goto taskkillconfirm
exit
:taskkillconfirm
color 0B
:image
set image=/im
if "%image%" == "%image%" goto imageconfirm
exit
:imageconfirm
color 0B
:forced
set forced=/f
if "%forced%" == "%forced%" goto forcedconfirm
exit
:forcedconfirm
:hour
color 0B
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.                                   Hours?:
echo.
set /p hour=                                      
:min
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                                   Minutes?:
echo.
set /p min=                                      
:sec
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                                   Seconds?:
echo.
set /p sec=                                      
:continue
color 0B
cls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo                           %taskkill%  Shutdown in
echo.                                                             
echo                         %hour%  Hours  %min%  Minutes  %sec%  Seconds
set /a sec="%sec%-1"
if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto done
ping -n 2 127.0.0.1 >NUL
goto continue
:done
color 0B
cls
taskkill %image% %taskkill% %forced%
exit

Also, what on earth does the above code even do?


Solution

  • Here is a quick batchscript I put together. It asks the user for input in minutes and converts the input to seconds to start the shutdown. It then stops the shutdown, if any key is pressed.

    custom_shutdown.bat

    @echo off
    title Custom Shutdown
    
    REM Ask the user for input and convert minutes to seconds
    set /P minutes="Enter shutdown delay in minutes: "
    set /A seconds="%minutes%*60"
    
    REM Start windows' shutdown command
    echo Starting shutdown...
    shutdown -s -f -t %seconds%
    
    REM Cancel shutdown on keypress
    echo.
    echo Press any key to cancel the shutdown
    pause>nul
    shutdown -a
    echo Shutdown canceled!
    
    pause
    exit
    

    It is not possible to use popups to ask the user for input with the standard batch commands. Using additional applications or VBScripts you can show popups. Check out this page for more information on popup messages and inputs.

    To change the size of the console window you can use the mode-command:

    mode con cols=60 lines=10
    

    There is not dedicated command to wait for some seconds but you can use the ping-command as one ping always takes one second. Because we don't need the command's output we discard it using >nul. Example code which waits for 5 seconds:

    ping -n 5 localhost>nul
    

    If you don't know what something means google helps for most batch problems. Also start with smaller programs and don't try using every function (input, math, choices etc.) in your first programs.