@ECHO OFF
REM
REM Windows command line batch script to to install a scheduled shutdown.
REM
SETLOCAL
REM Set the text strings.
SET MyTitle=Scheduled System Cleanup Restart
SET MyDescription=This is the message.
REM Prepare the shutdown command line.
REM Copy the following three lines into the uninstall script after they have been
REM changed.
SET MyComment=This is my comment.
SET OurShutdown=SHUTDOWN -r -t 180 -d up:241:50000 -c "%MyComment%"
SET OurValue=P;241;50000
REM Create the key, start the scheduler service if not running, and create
REM the scheduled task.
REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Reliability\UserDefined /v "%OurValue%" /t REG_MULTI_SZ /d "%MyTitle%\0%MyDescription%" /f
NET START schedule
REM Remove the double quotation marks for the comparisons.
SET OurShutdown=%OurShutdown:"=%
REM Check whether our task entry already exists.
REM Find the lines in the AT output that contain OurShutdown
AT>"%TEMP%\TheAtOutput.dat"
SET Terminate=No
FOR /F "eol=; tokens=* delims=," %%a IN ('TYPE "%TEMP%\TheAtOutput.dat"^|FINDSTR "%OurShutdown%"') DO CALL :Analyse %%a
IF "%Terminate%."=="Yes." GOTO :KeyEnd
REM Add the entry.
AT 09:00 /every:SUNDAY %OurShutDown%
GOTO :KeyEnd
:Analyse
REM Remove the quotation marks of the retrieved AT output lines.
SET AtLine=%*
SET AtLine=%AtLine:"=%
REM Take the first 4 characters and convert them into a number.
SET AtID=%AtLine:~0,4%
SET /A AtID=%AtID% + 0
IF "%AtID%."=="0." GOTO :EOF
SET AtID=%AtID: =%
REM Taks already exists. Exit.
ECHO The task already exists with ID %AtID%.
SET Terminate=Yes
GOTO :EOF
:KeyEnd
REM Clean up.
DEL /F/Q "%TEMP%\TheAtOutput.dat"
ECHO.
ECHO Finished.
REM Check for some common 'Quiet' switches.
IF /i "%1."=="/Q." GOTO :End
IF /i "%1."=="/SILENT." GOTO :End
ECHO Press any key to continue.
PAUSE>NUL
:End
ENDLOCAL