Share via

scheduler tasks with security options “Run whether user is logged on or not”.

YOFFE Mike 40 Reputation points
2026-02-26T14:32:57.9+00:00

We use the package <package id="TaskScheduler" version="2.12.2" targetFramework="net48" />

This is the code with three actions and its behavior:

|| | -------- | Task

|| | -------- | GUI

|| | -------- | Manually run from the scheduler

|| | -------- | Automatically run from the scheduler according to the trigger

Task

|| || GUI

|| || Manually run from the scheduler

|| || Automatically run from the scheduler according to the trigger

|| || Cmd echo

|| || No

|| || Run as expected

|| || Run as expected

|| || Notepad

|| || Yes

|| || Run in the background and does not work and not ended

|| || Run as in the trigger time in the background and does not work and not ended

|| || Our utility

|| || No

 

The same applications work fine if we use the option “Run only when user is logged on”

= = = = = = = = = = = = = = 

using (TaskService ts = new TaskService())

 {

     TaskDefinition td = ts.NewTask();

     td.RegistrationInfo.Description = "Run task as SYSTEM user";

 

     // ✅ Run whether user is logged on or not

     td.Principal.UserId = "SYSTEM";

     td.Principal.LogonType = TaskLogonType.ServiceAccount;

     td.Principal.RunLevel = TaskRunLevel.Highest;

 

     // Trigger: run once, 1 minute from now

     td.Triggers.Add(new TimeTrigger

     {

         StartBoundary = DateTime.Now.AddMinutes(1)

     });

 

     // Action

1.       td.Actions.Add(new ExecAction(@"C:\Windows\System32\cmd.exe /c ", 

"echo SYSTEM task ran >> C:\temp\system_task.log", null));

2.       td.Actions.Add("C:\Windows\notepad.exe");

3.       td.Actions.Add(new ExecAction(@"OurCustom.exe",”Our arguments”));

 

     // Register task

     ts.RootFolder.RegisterTaskDefinition( "Test", td, TaskCreation.CreateOrUpdate, null, null,

 TaskLogonType.ServiceAccount);

 

     //Console.WriteLine("Task created and runs as SYSTEM.");

 }

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments
{count} votes

Answer accepted by question author
  1. RLWA32 52,256 Reputation points
    2026-03-05T11:27:52.6633333+00:00

    @YOFFE Mike I broke out a very old virtual machine that had VB6 installed. I copied the VB6 console code from Creating a real console application in Visual Basic as well as the code to change the SUBSYSTEM.

    I removed all the code from the console application project and replaced it with the code to instantiate and use the scripting FileSystem object.

    Built the project and changed the resultant executable SUBSYSTEM . Then I copied the VB6 console application to a Win10 22H2 system and ran it as a scheduled task under SYSTEM account as described for the previous proof of concept.

    The VB6 console application completed successfully;y and created the test file in the C:\Users\Public\intergraph folder.

    I could not replicate your issue.

    0 comments No comments

Answer accepted by question author
  1. Q&A Assist
    2026-02-26T14:33:12.9733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    The behavior is by design for tasks configured to “Run whether user is logged on or not” under the SYSTEM (service) account.

    Key points:

    1. Desktop interaction is not available
      • When a task runs as SYSTEM with TaskLogonType.ServiceAccount and “Run whether user is logged on or not”, it runs in a non-interactive session.
      • GUI applications such as Notepad (or any app that expects a user desktop) cannot display a window or interact with the logged-on user. They appear to “run in the background” and typically hang or never complete properly.
      • This is why:
        • cmd /c echo ... works: it is a non-interactive console command that does not require a desktop.
        • notepad.exe and the custom utility (if it expects UI or interactive desktop resources) do not work and appear stuck.
    2. Why it works when “Run only when user is logged on”
      • In this mode, the task runs in the interactive user session with access to the user’s desktop.
      • GUI apps (Notepad, custom UI tools) can create windows and behave as expected.
    3. Recommended patterns
      • For background/system tasks:
        • Use console or service-style executables that do not depend on UI or desktop interaction.
        • Keep actions like cmd.exe /c ... or non-UI utilities.
      • For tasks that must show UI:
        • Configure the task to “Run only when user is logged on” and run under that user’s account, not SYSTEM.
      • Avoid running GUI apps under SYSTEM with TaskLogonType.ServiceAccount because they cannot reliably interact with any user session.
    4. Task Scheduler model
      • Task Scheduler tasks are defined by triggers and actions. Actions that execute command-line operations (like ExecAction with cmd.exe) are supported for non-interactive scenarios.
      • GUI actions are not suitable for non-interactive service accounts.

    To fix the current code:

    • Keep the cmd.exe action as-is for background logging.
    • Remove or replace the Notepad and any UI-dependent actions when using SYSTEM + “Run whether user is logged on or not”.
    • If Notepad or the custom utility must be visible, create a separate task configured as “Run only when user is logged on” under a real user account.

    References:


6 additional answers

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 14,955 Reputation points Microsoft External Staff Moderator
    2026-02-27T07:43:39.9366667+00:00

    Hi @YOFFE Mike ,

    Thanks for reaching out.

    Even though your application doesn’t have a GUI, Windows runs these tasks in a non-interactive session when no user is logged in. That session does not have access to your user desktop, profile folders, or mapped network drives, which can cause programs to hang if they rely on any user-specific resources.

    I would recommend a few approaches:

    1. Use absolute paths in your app - avoid user-relative paths like C:\Users\YourName\.... For configs or logs, use C:\ProgramData\YourApp\.
    2. Check permissions - make sure the SYSTEM account can read/write all folders and files your app needs.
    3. Avoid GUI-dependent code - even hidden windows or message boxes can make tasks hang.
    4. Test as SYSTEM - you can use Sysinternals’ psexec to run a command prompt as SYSTEM and verify your app works:
         psexec -i -s cmd.exe
      
    5. Consider a Windows Service - for non-interactive background tasks, services are often more reliable than Task Scheduler.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. YOFFE Mike 40 Reputation points
    2026-02-26T14:44:38.03+00:00

    TI am not satisfied with this answer.

    As I wrote, our application is not GUI. It make the following operations

    1. Read INI file
    2. Read the database
    3. Update the database

    NO any GUI operations but it does not work in the mode “Run whether user is logged on or not”.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.