Skip to main content

How to Schedule a Task to Run on Logon in a Batch Script

A "logon task" is a program or script that runs automatically every time a specific user (or any user) logs into Windows. This is one of the most common and powerful automation triggers, used for tasks like mapping network drives, setting up a user's environment, or launching startup applications.

This guide will teach you how to use the built-in schtasks.exe command-line utility to create a scheduled task that triggers at logon. You will learn the correct syntax, how to specify the user context for the task, and how to create a robust script to automate this setup.

danger

CRITICAL NOTE: Creating a system-wide scheduled task is a privileged operation. To create a task that runs for any user or as a different user, you must run your script with full administrator privileges.

The Core Command: schtasks /CREATE

The schtasks.exe utility is the standard command-line tool for managing scheduled tasks. The /CREATE action is used to register a new task. To make it a logon task, we use a specific schedule type.

The Key Parameter: /SC ONLOGON

The /SC switch specifies the Schedule type. While common values include DAILY or HOURLY, the special ONLOGON value creates a trigger that fires whenever a user logs on.

Syntax: schtasks /CREATE /TN "Task Name" /TR "Task To Run" /SC ONLOGON

  • /TN "Task Name": The Task Name for your new task.
  • /TR "Task To Run": The Task Run command (the script or program to execute).
  • /SC ONLOGON: The schedule type.

Basic Example: Running a Script for the Current User

This is the simplest form. The script creates a task that will run for the user who creates the task, every time they log on. This can often be run without administrator rights if you are creating the task for yourself.

@ECHO OFF
SET "TaskName=My User Logon Script"
SET "TaskCommand=C:\Scripts\UserSetup.bat"

ECHO --- Creating a user-specific logon task ---
ECHO Task Name: "%TaskName%"
ECHO.

schtasks /CREATE /TN "%TaskName%" /TR "%TaskCommand%" /SC ONLOGON /F

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Task created successfully.
) ELSE (
ECHO [FAILURE] An error occurred.
)
  • /F: The Force switch is used to overwrite the task if it already exists, making the script safely re-runnable.

Now, the UserSetup.bat script will run automatically every time the current user logs in.

Running a Task for Any User That Logs On

A more common administrative task is to create a single task that runs for any user who logs onto the machine. This is perfect for setting up a consistent environment on a shared computer or a server.

To do this, you must specify that the task should run with the permissions of a high-privilege account, like the SYSTEM account.

@ECHO OFF
REM This script MUST be run as an Administrator.

SET "TaskName=\System\GlobalLogonTask"
SET "TaskCommand=C:\Scripts\GlobalSetup.bat"

ECHO --- Creating a global logon task for all users ---
ECHO.

REM The /RU SYSTEM switch makes this a global, system-level task.
schtasks /CREATE /TN "%TaskName%" /TR "%TaskCommand%" /SC ONLOGON /RU SYSTEM /F

ECHO Task creation command issued.

When this task runs, the GlobalSetup.bat script will be executed with SYSTEM privileges every time any user logs on.

Key schtasks Parameters for Logon Tasks

  • /SC ONLOGON: (Required) Sets the trigger type.
  • /TN <TaskName>: The name of the task. Using a subfolder like \MyTasks\ is good practice.
  • /TR <TaskRun>: The command or script to execute.
  • /RU <UserName>: Run as User.
    • If omitted, it defaults to the user running the schtasks command.
    • Use SYSTEM to create a task that runs for any user logon.
  • /RL <Level>: Run Level. Use /RL HIGHEST to ensure the task runs with the highest available privileges (i.e., as an administrator if the user is an admin).

Common Pitfalls and How to Solve Them

  • Administrator Rights: This is the number one cause of failure. If you are creating a task that runs as SYSTEM or for all users, you must run your script from an elevated ("Run as Administrator") command prompt.

  • Interactive vs. Non-Interactive: A task running as SYSTEM at logon runs in a non-interactive session (Session 0). This means it cannot display any GUI elements. It cannot open Notepad or show a pop-up message. Logon tasks running as SYSTEM must be silent, background processes.

    • Solution: If you need an interactive program to run when a user logs in, the task must be configured to run as that user, not as SYSTEM.
  • Task Already Exists: If you run the CREATE command twice, it will fail the second time.

    • Solution: Always use the /F (Force) switch in your scripts. This will overwrite any existing task with the same name, ensuring your script can be run multiple times to enforce the correct configuration.

Practical Example: A Script to Map Network Drives at Logon

This script creates a task for a specific user (jdoe) that runs a separate script (MapDrives.bat) every time that user logs in.

@ECHO OFF
SETLOCAL
REM This script MUST be run as an Administrator.

SET "TargetUser=CORP\jdoe"
SET "TaskName=Map Network Drives for jdoe"
SET "RunScript=C:\Scripts\MapDrives.bat"

ECHO --- Creating logon task for user: %TargetUser% ---
ECHO.
ECHO This task will run '%RunScript%' every time the user logs on.
ECHO You will be prompted for the user's password.
ECHO.

REM The /RP switch with no password will cause a secure prompt.
schtasks /CREATE /TN "%TaskName%" /TR "%RunScript%" /SC ONLOGON /RU "%TargetUser%" /RP /F

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Logon task created successfully.
) ELSE (
ECHO [FAILURE] Failed to create the task.
ECHO Common causes: Incorrect password, user not found, or not running as Admin.
)

ENDLOCAL
PAUSE

Conclusion

The schtasks command is the definitive tool for automating the creation of logon tasks, a cornerstone of user environment management.

For reliable logon task creation:

  1. Use the schedule type /SC ONLOGON.
  2. Run your script as an Administrator for any task that needs to run as SYSTEM or for a different user.
  3. For a task that runs for any user, set the Run As user to /RU SYSTEM.
  4. Always use the /F switch to make your creation script re-runnable.

By mastering the /SC ONLOGON trigger, you can create powerful scripts that configure user environments automatically and reliably.