Windows: Customize Autostart Programs and Tasks with schtasks.exe

Bruce Wen
3 min readNov 20, 2021

SCHTASKS (schtasks.exe) is one powerful tool to ask Windows Administrator to manage scheduled tasks in local or remote system. This article is to tell how to customize autostart programs with schtasks.exe.

Photo by James Harrison on Unsplash

schtasks.exe locates under C:\Windows\System32.

To use it, you must have Administrator privilege.

To create one scheduled task, we use /create sub-command, run below command to get its help document:

.\schtasks.exe /create /?

To create a local scheduled task, we need to provide below options:

  • /SC — schedule, specifies the schedule frequency. Valid schedule types: MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONSTART, ONLOGON, ONIDLE, ONEVENT. As we want to customize autostart programs, we choose ONLOGON. Of course, you can use other schedule types as you want.
  • /TN — taskname, specifies the string in the form of path\name which uniquely identifies this scheduled task. We can just use ‘AutostartPrograms’.
  • /TR — taskrun, specifies the path and file name of the program to be run at the scheduled time. This will be the absolute path of our script file which defined what to run.
  • /RU — username, specifies the “run as” user account (user context) under which the task runs. We can use our own account username.

Assuming our script is C:\Users\USERNAME\autostart.bat , then the command to create the scheduled task will be:

.\schtasks.exe /create /tn AutostartPrograms /tr c:\Users\USERNAME\autostart.bat /ru USERNAME /sc onlogon

(📓Note: replace USERNAME with your account username)

Now, it’s time to prepare the script file c:\Users\USERNAME\autostart.bat

For example, I want to start Opera browser, Notepad++, Virtualbox, Teams, MS OneNote, MS Outlook.

Then, I just need to put below lines in the autostart.bat file:

"C:\Users\USERNAME\AppData\Local\Programs\Opera GX\opera.exe"
"C:\Program Files\Notepad++\notepad++.exe"
"C:\Program Files\Oracle\VirtualBox\VirtualBox.exe"
"C:\Users\USERNAME\AppData\Local\Microsoft\Teams\current\Teams.exe"
"C:\Program Files (x86)\Microsoft Office\root\Office16\ONENOTE.EXE"
"C:\Program Files (x86)\Microsoft Office\Root\Office16\OUTLOOK.EXE"

To test if the created scheduled task can run, we can try to run it manually:

.\schtasks.exe /run /tn AutostartPrograms

We can also do more things in the autostart.bat file. For example, we can start Virtual Machine:

VBoxManage.exe startvm Fedora_64

If needed, I can do more complex things. For example, we can create PowerShell script to include what we want to run, and then call the PowerShell Script in autostart.bat file.

For example, I want to show all new generated system error event logs today when I logon. Then I can write PowerShell script autorun.ps1 with below content:

$sys_error_log_filename = C:\users\USERNAME\syserror.log
$system_error_logs = Get-EventLog -EntryType Error -LogName System -After $(get-date -UFormat +%Y-%m-%d)
$system_error_logs | foreach-object {
echo $_ ToString >> $sys_error_log_filename
}
notepad $sys_error_log_filename

📓 To check application error event logs, change System to Application.

Get-EventLog -EntryType Error -LogName Application -After $(get-date -UFormat +%Y-%m-%d)

In autostart.bat file, I just need to add below line to call the PowerShell script file:

PowerShell -File c:\Users\USERNAME\autorun.ps1

OK, by using schtasks.exe and script skill in BATCH file and PowerShell script, I believe that you can create your own scheduled task to customize autostart programs and tasks.

Other Tips

  • Query tasks: if you forgot the name of the task, but you know which script is to run, then query by command “schtasks /query /xml > all_tasks.xml” and then find the task details in the file all_tasks.xml by searching the script name.

Thanks for reading and happy coding!

References

--

--