1

I wanted to write a simple batch script to shutdown my own computer which will be used when I'm in bed watching a DVD or something.

UPDATE: fixed the code, any other suggestions?

setlocal
@echo off & break off
:input
set /p "minutes=Enter number of minutes to wait until shutdown: "
set "numcheck="&for /f "delims=0123456789" %%i in ("%minutes%") do set "numcheck=%%i"""
if defined numcheck (
    echo ERROR: Sorry "%minutes%" is not numeric, please try again & echo.
    goto input
)
if %minutes% gtr 315360000 (
    echo ERROR: Sorry your input is greater then 10 years, I can't handle that & echo.
    goto input
)
set /a "seconds=%minutes%*60"
shutdown.exe /s /f /t "%seconds%"
if errorlevel 1 (
    echo ERROR: Could not set shutdown, try again & echo.
    goto input
)
:abort
set /p "continue=Your computer is about to shutdown in %minutes% minutes, do you want to abort? (yes/no): "
if %continue% equ yes (
    echo Aborting...
    shutdown.exe /a
    if errorlevel 1 (
        echo ERROR: Could not stop shutdown, try again & echo.
        goto abort
    )
    timeout.exe /t 5 /NOBREAK
    endlocal
    exit /b
) else (
    timeout.exe /t 10 /NOBREAK
    endlocal
    exit /b
)

2 Answers2

2

The most common ways to use the shutdown command are:

  • shutdown -s — Shuts down.

  • shutdown -r — Restarts.

  • shutdown -l — Logs off.

  • shutdown -h — Hibernates.

    Note: There is a common pitfall wherein users think -h means "help" (which it does for every other command-line program... except shutdown.exe, where it means "hibernate"). They then run shutdown -h and accidentally turn off their computers. Watch out for that.

  • shutdown -i — "Interactive mode". Instead of performing an action, it displays a GUI dialog.

  • shutdown -a — Aborts a previous shutdown command.

The commands above can be combined with these additional options:

  • -f — Forces programs to exit. Prevents the shutdown process from getting stuck.

  • -t <seconds> — Sets the time until shutdown. Use -t 0 to shutdown immediately.

  • -c <message> — Adds a shutdown message. The message will end up in the Event Log.

  • -y — Forces a "yes" answer to all shutdown queries.

    Note: This option is not documented in any official documentation. It was discovered by these StackOverflow users.


I want to make sure some other really good answers are also mentioned along with this one. Here they are in no particular order.

0

Shutdown -s -t100 Save it with .bat extenstion Shutsdown in 100 secx

pavan
  • 34
  • 2
  • 10