Skip to content

Instantly share code, notes, and snippets.

@Laforeta
Last active August 29, 2015 14:25
Show Gist options
  • Save Laforeta/34f75d638ffa756b30b5 to your computer and use it in GitHub Desktop.
Save Laforeta/34f75d638ffa756b30b5 to your computer and use it in GitHub Desktop.
A simple shutdown script with menu and error detection
@ECHO OFF
SETLOCAL
SET ME=%~n0
SET PARENT=%~dp0
ECHO Running %ME%...
:MENU
ECHO.
ECHO #####################
ECHO Laf's Shutdown Script
ECHO #####################
ECHO.
ECHO 1 - Log out the current user now
ECHO 2 - Shut down this computer now
ECHO 3 - Set a timer to shutdown this computer
ECHO 4 - Cancel a previously set shutdown timer
ECHO 0 - Exit this menu
ECHO.
SET /P OPTION=Choose from one of the options above and press enter:
IF %OPTION%==1 (
CALL :LOGOUT
) ELSE IF %OPTION%==2 (
CALL :SHUTDOWN_NOW
) ELSE IF %OPTION%==3 (
CALL :SHUTDOWN_TIMER
) ELSE IF %OPTION%==4 (
CALL :SHUTDOWN_CANCEL
) ELSE IF %OPTION%==0 (
ECHO The script will now quit
GOTO EXIT
) ELSE (
ECHO Please choose one valid option from the list
PAUSE
GOTO MENU
)
GOTO EXIT
:LOGOUT
ECHO PLEASE confirm that you wish to log out the current user
PAUSE
ECHO The current user will be logged out in 8 seconds
TIMEOUT /t 8 /NOBREAK
echo shutdown -l
GOTO:EOF
:SHUTDOWN_NOW
ECHO Please confirm that you wish to shut down this computer immediately
PAUSE
ECHO Computer shutting down in 8 seconds
TIMEOUT /t 8 /NOBREAK
echo shutdown -s -t 0
GOTO:EOF
:SHUTDOWN_TIMER
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO Please enter the number of minutes you wish to set before the computer is shut down
SET /p TIMER_MIN=
IF %TIMER_MIN% leq 0 (
ECHO Please enter a valid time in minutes
GOTO SHUTDOWN_TIMER
) ELSE (
SET /a TIMER_SEC= 60*%TIMER_MIN%
echo shutdown -s -t !TIMER_SEC!
)
ENDLOCAL
GOTO:EOF
:SHUTDOWN_CANCEL
echo shutdown -a
IF %errorlevel% neq 0 (
ECHO An error has occured while cancelling the shutdown timer
) ELSE (
ECHO Shutdown timer has been successfully cancelled
)
GOTO:EOF
:EXIT
ENDLOCAL
ECHO Shutdown script executed successfully and will now exit
EXIT /b 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment