For batch you can use the /create
sub-command of the schtasks
command to schedule a task. The schtasks
is a command that allows you to manage and create scheduled tasks. The /create
sub-command will create scheduled tasks.
Before giving you a solution for your request, I'd recommend you to read the windows help message with schtasks /create /?
or through the link I added for the schtasks
command. Both provide explanation on the different options available when creating scheduled tasks and provide some examples. It is easy to create scheduled tasks, but when one causes problems (for eg. it isn't configured the way you really want it), it might be a real headache to find and solve the issue.
For your case the easiest way to do it would be something like:
SCHTASKS /create /tn "Curfew" /tr "C:\Windows\System32\shutdown.exe /s /f" /sc daily /st 22:00
This will create a task that will shutdown the system each day at 10 PM.
EDIT: explanation of the command:
- the
/tn
option gives a name to the scheduled task
- the
/tr
option is used to specify the command the task has to execute
- the
/sc
option specifies the frequency of the task
- the
/st
option specifies for which time of the day the task must be scheduled
Again, for more explanation on these options and the command itself, please read the help message (schtasks /create /?
) or follow the link
Some other useful examples of schedule types can be found here
EDIT 2: Task to shutdown system if it has been started between 10.PM and 06.AM (see comment OP)
You'll need a batch script that shuts down the system when it is between 10.00 PM and 06.00 AM:
@echo off
REM Get current timestamp from system
FOR /F "tokens=2 delims==." %%G in ('wmic os get LocalDateTime /VALUE') DO set timestamp=%%G
REM Extract hours from timestamp
set hour=%timestamp:~8,2%
REM Verify if hour >= 22 or hour < 6
set shutdown=
IF %hour% GEQ 22 set shutdown=true
IF %hour% LSS 6 set shutdown=true
REM Shutdown system if so
IF "%shutdown%" == "true" shutdown /s /f
It uses wmic os LocalDateTime
to get the current timestamp instead of %time%
because it has the advantage to be in ISO format and doesn't depend on locale settings as %time% does (see this answer). Save that as a batch script, for eg. as C:\Windows\Curfew_police.bat
or a "less obvious" name if you want but it is important that the script remains accessible for any user on the system. Then you can schedule a task that will execute the script each time the system starts with /sc onstart
:
schtasks /create /tn "Curfew police" /tr "\"c:\path\to script\batch.bat\"" /sc onstart
Replace the c:\path\to script\batch.bat
with the real path to the batch script above (C:\Windows\Curfew_police.bat
for eg.)