How to Disable/Enable Scheduled task through php


I want to enable and disable scheduled task through my php code.

I tried

$cmd = "c:\\windows\\system32\\schtasks.exe /Change /TN openChrome /Enable";
$output = shell_exec($cmd);
echo $output;


$cmd = "schtasks /Change /TN openChrome /Enable";
$output = shell_exec($cmd);
echo $output;

These codes work fine when I write code for Run or End a task. But to Enable or Disable tasks they do nothing.

What is wrong in code to Enable/Disable? I am Using windows 8.1 and xampp. Are these administrator commands? If these are administrator commands. then how to execute administrator commands from php file.


Command is:

SCHTASKS /Change [/S system [/U username [/P [password]]]] /TN taskname
     { [/RU runasuser] [/RP runaspassword] [/TR taskrun] [/ST starttime]
       [/RI interval] [ {/ET endtime | /DU duration} [/K] ]
       [/SD startdate] [/ED enddate] [/ENABLE | /DISABLE] [/IT] [/Z] }


$cmd = “schtasks.exe /Change /TN openChrome /ENABLE /RU”;
$cmd = “SCHTASKS /Change /TN openChrome /ENABLE /RU”;

Also, I found that sometimes you need to reset the task scheduler. If so, send these out:

net stop Task Scheduler
net start Task Scheduler

It appears to make a difference depending on your user set up on the local system. Passworded or not.
The first one should do it. Just add the /RU so it runs as the current user. Hope this works for you.


Thanks for your detailed reply.


After spending almost 5 days on it I found a solution to do this. To Enable/Disable task on Task scheduler you have to run Enable/Disable (“c:\windows\system32\schtasks.exe /Change /TN openChrome /Enable”) command on administrator privileges. One could run administrator commands by running command prompt as administrator. But it is difficult to run administrator commands from php.
So for both, either you want to run cmd.exe commands or schtasks.exe commands as administrator from php, you just need to copy cmd.exe or schtasks.exe files from C:\Windows\System32 and paste to some other directory. For Example, I paste that to D:\ drive.
Now right click on that.
Click properties.
Go to Compatibility tab.
Check “Run this program as an administrator”.
Click OK.
Now go to your PHP file and use that path before command where you pasted cmd or schtasks file. (i.e, in my case I pasted SCHTASKS file in D:. So, now my command $cmd = “D:\SCHTASKS /Change /TN openChrome /Enable”).

This method run all your SCHTASKS or cmd commands as administrator from PHP exec() or shell_exec() functions.

Note: The only purpose of coping file from C:\Windows\System32 is, in that folder you can’t find compatibility tab in properties and there is no option to run always as administrator. I don’t know whether this method is secure or not.


Or just run it as ADMIN…

runas /U username /P password /D Domain /E rest of code

Add this to the beginning of your command and enter your admin’s username and password.
This is often just administrator and no password or your login if you have the computer password protected