1

And i want a script to be put on start up folder
and not use the task scheduler

    If time >= 22:00 hrs   // shutdown if startup after 10pm
      Shutdown /s /f
    break

    While (time != 22:00hrs) //looping until 22:00hrs
    Any statement
    Else 
      Shutdown /s /f
    Exit
Glenn_ford7
  • 41
  • 1
  • 8
  • This job doesn't require VBScript. I believe a batch script **or** an task schedule work fine. –  Oct 14 '17 at 04:03
  • A batch file or vbs will do but my problem is i dont know .bat and .vbs coding – Glenn_ford7 Oct 14 '17 at 04:11
  • How about task scheduler? No coding required. –  Oct 14 '17 at 04:11
  • Users may find the task and delete it if they want... if in a script maybe i can hide it rename it or just a shortcut of it on a startup folder... by the way thanks for your advice but i think i have to use a script on it – Glenn_ford7 Oct 14 '17 at 04:17
  • Do you know you can lock a program? –  Oct 14 '17 at 04:19
  • Sounds good, i dont know that stuff – Glenn_ford7 Oct 14 '17 at 04:21
  • Are you creating a virus? Generally users have full control over the computer. – user202729 Oct 14 '17 at 08:19
  • Its not creating a virus i just want members of my family not to stuck up on computer very late at night – Glenn_ford7 Oct 14 '17 at 08:46
  • 1
    _If members of your family cannot do as they are asked, then learn to be a more respected family member instead of a script writer_. I would suggest that they're less likely to check through scheduled tasks than look for something in their startup folder. Also scheduled tasks can be protected by using group policy, you can enable both Prevent Task Run or End and Prohibit Task deletion. – Compo Oct 14 '17 at 10:01
  • 1
    Windows has parental controls for that. – Squashman Oct 14 '17 at 15:26

3 Answers3

2

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.)

J.Baoby
  • 2,167
  • 2
  • 11
  • 17
  • 1
    Be aware of the fact that `shutdown /s /f` won't even give the user the chance to save some unfinished work. Maybe you could consider adding a time-out with the `/t` option? – J.Baoby Oct 14 '17 at 10:40
  • Thanks for your very helpful answer and also for the link , i hav to develop some codes for the " if " statements and " while loop" from this... – Glenn_ford7 Oct 14 '17 at 11:05
  • Your whole while-loop can be replaced with the scheduled task above. But if I understand your if statement correctly, you want to make the system shutdown if it has been started after 10PM? – J.Baoby Oct 14 '17 at 11:09
  • Yes.... exactly.... i want the system to shutdown again if started after 10pm... the thing bothers me also if it started at 00:01hrs ... thats why i add the condition " >= " – Glenn_ford7 Oct 14 '17 at 11:17
  • there is a schedule type `/sc onstart` that allows you to schedule a task for each time the system starts (see [this example][https://technet.microsoft.com/en-us/library/cc725744(v=ws.11).aspx#BKMK_startup] ). That can be usefull. But as you said, a start up at 00:01 won't be affected. From what time would you accept the system to start normally without shutting down? – J.Baoby Oct 14 '17 at 11:24
  • I want the system to start normally at 06:00am and shutdown at 10pm, so from 22:01 to 05:59 if started at this time range it should shutdown automatically.... i think the condition on my "IF " statement cannot met this config, because 00:01 is less than 23:00 – Glenn_ford7 Oct 14 '17 at 11:43
  • It is possible but will require an additional task to be scheduled with some batch processing. I'll edit my answer – J.Baoby Oct 14 '17 at 12:08
  • Nice info, the /sc will do the onstart and daily, i just add two " if " statements with arguments on %time% – Glenn_ford7 Oct 14 '17 at 12:22
  • @Glenn_ford7 I've edited my answer and added a script for the IF statement as well – J.Baoby Oct 14 '17 at 12:41
  • 1
    Many thanks for your code... ive already test it.... ive only put the batch file above, in the startup folder and it worked.... i think i have to add -t for in shutdown .exe to execute it in 5 seconds or so ... so that command line will exit in no time – Glenn_ford7 Oct 14 '17 at 13:30
0

Instead of using the scheduler you could also move your file to the startup folder in your batch file and count down. It does require you to be an administrator, though.

@echo off
::sets the hour to a varible called hour
set hour=%TIME:~0,2%

::copy the file only if it doesn't exist.
::This is important because we don't want to copy the file every time the computer starts up.
if NOT EXIST "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\%0" (
copy "%cd%\%0" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\%0"
)
::if you want this to only pertain to you, replace:
::"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\%0"
:: with "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%0" both times.


:loop
if "%hour%" NEQ "22" (
::wait one minute, sence windows %time% command doesn't have seconds
sleep 60
set hour=%TIME:~0,2%
) else (
shutdown /s /f
)
goto :loop

Note how he said without the scheduler, thus maybe his task scheduler is broken so he needs to use the startup folder instead.

  • Thats it... creating a task scheduler always fail on my machine... that why i have to use third party scheduler and its been obvious to them already. This batch file is hard for them to read and its the best way i think...@Z.mikaels. .. thanks for this also – Glenn_ford7 Oct 15 '17 at 02:00
  • The `%time%` variable is locale-dependent, and may not work on some machines. The sleep command is also decrepeated, and it's replacement shall be timeout. –  Nov 01 '17 at 09:47
0

Make a vbs file called turnoffafterxMins.vbs with this code

Set WshShell = WScript.CreateObject("WScript.Shell")
Dim batName 
Dim statusCode
Dim returntext
returntext = InputBox("How long? (in minuites)", "How long?", "60", 150, 150)

if IsNumeric(returntext) Then
returntext = returntext*1000*60
WScript.Sleep returntext
batName = "C:\Users\The_SUDO\Desktop\shutdown.bat"
statusCode = WshShell.Run (batName, 1, true)
MsgBox returntext
else
batName = "C:\Users\The_SUDO\Desktop\turnoffafterxMins.vbs"
statusCode = WshShell.Run (batName, 1, true)
end if

Then make a batch file called shutdown.bat with this code

shutdown.exe /s /f