Start service from Task Scheduler as minimised

16,342

Solution 1

The problem is that when you configure the shortcut to run minimized you launch minimized only the program that starts the scheduled task, not the program that the task executes. You need another piece in your Rube Goldberg machine launch sequence, make the following changes:

  • Program\script = C:\Windows\System32\cmd.exe
  • Add arguments (optional) = /c start /min net start "SERVICE-NAME"

The first "start" is the cmd.exe start command, which we use for two functions:

  1. Launch net.exe minimized.
  2. Make taskeng.exe finish as soon as net.exe is launched instead of waiting for it to finish.

Note that this solution is not perfect as taskeng.exe will flash briefly until the net.exe is launched, but usually less than a second.

Solution 2

You can start a service (or any other program) without any window flashing at all, not even minimized.

Create a task:

  • Command/script: mshta.exe
  • Add arguments (optional) :
    "javascript: new ActiveXObject('shell.application').ShellExecute('net.exe', 'start <SERVICE-NAME>', '', 'open', 0); close();"

Since you want to start a service, you need administrative rights, so check Run with highest privileges at the first tab.

To run the task without showing any window, you use the same trick. Create a shortcut with the following command:

mshta.exe "javascript: new ActiveXObject('shell.application').ShellExecute('schtasks.exe', '/run /tn <TASK-NAME>', '', 'open', 0); close();"

Notes

  • The user that runs the shortcut must be an admin because normal users can run only their tasks and this task is one with highest privileges. Otherwise, the schtasks command will fail with "access is denied".
  • If the task is in a task folder, the /tn <TASK-NAME> is /tn <FOLDER>\<TASK> from the shell, and you must double the backslash to escape it in the mshta command of the shortcut.
  • Some antivirus programs (at least Kaspersky) may consider a task using mshta this way as suspicious or a potential risk, and thus delete it.
  • You can use this trick to run any program hidden without popping up whatsoever. Just replace net.exe with the name of your program and the start <SERVICE-NAME> part with the needed parameters.
Share:
16,342

Related videos on Youtube

AlainD
Author by

AlainD

By day: Software Engineer using C/C++, Delphi and C#. By night: Musician; World champion of boardgames including Entropy, Lines of Action, Pacru, and Azacru; Represented South Africa and United Kingdom at international Chess, Xiangqi and Backgammon.

Updated on September 18, 2022

Comments

  • AlainD
    AlainD almost 2 years

    After creating a scheduled task after following this Superuser question, I can successfully start a service without the UAC prompt. The options selected were:

    • Start a program
    • Program\script = C:\Windows\System32\net.exe
    • Add arguments (optional) = start "SERVICE-NAME"

    Then in a batch file I call schtasks /run /tn TASK-NAME. After testing this, I created a shortcut to the batch file and set the Run option to Minimised.

    This all works except that when the actual service is started, a maximised command prompt window pops up with the single line The SERVICE-NAME service is starting. This then disappears after a few seconds.

    How do I tell Task Scheduler to start the target program silently (or minimised) without the pop-up? If the pop-up command prompt were minimised that would be fine.

    • Vomit IT - Chunky Mess Style
      Vomit IT - Chunky Mess Style almost 7 years
      On the scheduled task, be sure the option Run whether user is logged on or not is selected as well which I didn't see mentioned in the linked post... check here for common troubleshooting guidance as well: superuser.com/questions/1005192/…
  • AlainD
    AlainD almost 7 years
    Excellent, thanks. I briefly tried to use start but couldn't get the syntax right. This works perfectly. There is a brief flash as the action (net start SERVICE-NAME) is launched, but very short.
  • Alberto Martinez
    Alberto Martinez almost 7 years
    Yes, taskeng.exe is what you say.
  • stevek_mcc
    stevek_mcc about 6 years
    You need to add "" after the first START, or it will misparse any quoted argument as a window title, and give an error 'The system cannot find the file' for the next argument. Use /c start "" /min net start "SERVICE-NAME".
  • Alberto Martinez
    Alberto Martinez about 6 years
    @stevek_mcc I tested it before posting the answer and worked fine without adding the extra quotes.
  • stevek_mcc
    stevek_mcc about 6 years
    @AlbertoMartinez Ah, I see the difference - thanks: my command wasn't start /min net start "..." but start /min "c:\Program Files\..." - START mis-parses if the first argument after the /options has quotes, but parses OK for quotes later.
  • Joachim Otahal
    Joachim Otahal about 4 years
    I prefer that one, it briefly takes away the focus like the flashing .cmd version, but there is no window flashing up and no minimized task visible. Try with mshta "javascript: new ActiveXObject('shell.application').ShellExecute('ping.exe', '-n 10 127.0.0.1', '', 'open', 0); close();" and you will see ping for ten second only in the task manager.
  • Joachim Otahal
    Joachim Otahal about 4 years
    That even works on Windows XP and Windows 2000! Just tested out of curiosity...