59

I've got a daemon process that I run on my machine every hour, and even though I've checked the Hidden box, it doesn't work.Task Properties

Every hour, the task runs, but it shows the black command window, in which my .NET Console app is running. This stays visible until the task completes, and then disappears. This is very annoying, because it pops up and interrupts whatever I'm doing:

enter image description here

I really do want it hidden, so how can I fix this?

Joshua Frank
  • 1,671

9 Answers9

67

Check the "Run whether user is logged on or not" check box and executable will run in the background.

Fern
  • 679
30

Why is Windows 7 scheduled task running hidden? should explain why this happens.

Also, this TechNet description of General Task Properties states:

The general properties of a task are displayed on the General tab of the Task Properties or Create Task dialog box. The general properties of a task include the task name, author, description, security options, the operating system that the task is configured for, and a property that controls whether the task is hidden.

When you create a task in a task folder, you must name the task. No two tasks can have the same name if they are in the same task folder. The task description is optional.

Task security options specify the security context that the task runs under. For more information, see Task Security Context.

By default, hidden tasks are not shown in the Task Scheduler user interface. You can view hidden tasks when Show Hidden Tasks is selected in the View menu. You make a task hidden when you click the Hidden check box on the General tab of the Task Properties or Create Task dialog box.

Doesn't this imply that the checkbox only hides the task itself from the scheduler's UI, and not the actual program that's scheduled?

Karan
  • 57,289
22

Okay, after some more research I found this answer on StackOverflow:

To setup a Scheduled Task to run in background, change the User running the task to SYSTEM, and nothing will appear on your screen.

A comment points out that this grants full rights to the task, which could be annoying, but is acceptable in this case.

But still, what does Hidden do, if it doesn't do this? And why would the user account running the program have anything to do with whether it's visible?

Joshua Frank
  • 1,671
16
  • Changing the User running the task to SYSTEM - will execute your command not under your user
  • Checking the "Run whether user is logged on or not" - requires to save password with the task, and will hiddenly break next time password changes (what usually enforced by domain policies in enterprise environments)

If those are deal breakers for you, the ultimate solution is to create JS script file like the one below, that will run your command in hidden window, wait for your command to terminate and return its error code:

WScript.CreateObject("WScript.Shell")
.Run('powershell -File C:\\Path\\To\\MyScript.ps1', 0,true);

Save this as js file (myjob.js) and add this to your task scheduler: enter image description here Enjoy

4

This answer is a bad security practice. If the task does not need system-level privileges, it should not have them.

This answer from another thread is a much better option.

Use the following when creating/editing a "Start a program" action in the Task Scheduler.

powershell "start <executable path> -WindowStyle Hidden"

Place powershell in the "Program/script" field and the following string (start <path> -WindowStyle Hidden) in the "Add Arguments (optional)" field.

image: editing a task scheduler action

ChanganAuto
  • 1
  • 4
  • 18
  • 19
3

I leave the default settings and write short custom programs that spawn the execution without a window showing. For example, to run a batch file without popping up a command prompt window, use the code below. Set the task to spawn "RunBatchFile.exe (path_to_batch_file)".

class Program
{
    static void Main(string[] args)
    {
        try
        {
            if (args.Length < 1)
            {
                Console.WriteLine("No batch file");
                Console.WriteLine("Usage: RunBatchFile (path)");
                Environment.Exit(1);
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("Batch file {0} not found", args[0]);
                Console.WriteLine("Usage: RunBatchFile (path)");
                Environment.Exit(1);
            }

            // Set to batch file's folder
            string startIn = Path.GetDirectoryName(args[0]);
            if (!string.IsNullOrEmpty(startIn) && Directory.Exists(startIn))
                Directory.SetCurrentDirectory(startIn);

            // Start
            ProcessStartInfo pi = new ProcessStartInfo()
            {
                FileName = args[0],
                UseShellExecute = true,
                WindowStyle = ProcessWindowStyle.Hidden
            };
            Process.Start(pi);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: " + ex.ToString());
        }
    }
}
2

"Run whether user is logged on or not" will allow the hidden option to work. Note that you need provide your credentials after you choose this.

Heptite
  • 20,411
1

Both :

  • Changing the User running the task to SYSTEM"
  • and Checking the "Run whether user is logged on or not"

will prevent the black command window, but be aware of the drawbacks : you won't be able the work with some GUI automation tools like 'AutoIt' or SendKeys in PowerShell, as Windows launch the tasks in a headless style environment.

0

I was having this problem with powershell scheduled tasks (that I was deploying through GPO). I ended up wrapping it in a command window:

CMD
/Q /D /C "powershell.exe -Command "& '\\MYDOMAIN\netlogon\MyPowerShellScript.ps1'""