53

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?

5
  • possible duplicate of Stop taskeng.exe window from popping up Sep 22, 2012 at 17:48
  • 2
    The linked question doesn't mention the Hidden setting.
    – bwDraco
    Sep 22, 2012 at 20:01
  • @DragonLord You are correct, but it's hard to tell if this question is "Why doesn't Hidden work?", or if it's actually "How do I stop this box from showing?". Sep 23, 2012 at 14:05
  • @techie007: The question is how to make the window hidden, which is what I thought the hidden checkbox does. But that linked article deals with scripts and the command line task scheduler and shell scripts, not the task scheduler GUI shown in my post. Do you know how to use the GUI to accomplish this? Sep 24, 2012 at 12:25
  • In most cases you can't do it just from the Task Scheduler GUI. Usually you need to modify the what's being run by the task's Action (ie: a batch file or script) to hide the sub-window that's popping up. That's what the other question covers, and that's why I flagged it as a possible dupe. :) Sep 25, 2012 at 3:03

9 Answers 9

66

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

5
  • 4
    As simple as that. This answer should be the accepted one.
    – rolve
    Dec 31, 2013 at 11:56
  • Thank you. I had the opposite problem. I wanted the console to display, but it wasn't! Dec 11, 2014 at 21:44
  • 14
    This requires to save password with the task, and will hiddenly break next time password changes (what usually enforced by domain policies in enterprise environments). Feb 10, 2016 at 17:19
  • @PhilippMunin In certain use-cases, you may be able to get away with ticking the checkbox for 'Do not store password. The task will only have access to local computer resources.', which would obviate supplying the password.
    – Jimadine
    Aug 22, 2019 at 15:15
  • Running without window with this option is only a side effect and breaks things that require user context
    – John Smith
    Dec 27, 2022 at 14:51
28

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?

4
  • It does make it sound that way, but it doesn't do that either; I can see the tasks in the Scheduler. It would be an odd UI that worked with objects that you couldn't see. Sep 21, 2012 at 18:58
  • 2
    Do you have Show Hidden Tasks checked in the View menu?
    – Karan
    Sep 21, 2012 at 19:02
  • 2
    Ah, I did, so that explains it. Thanks. Of course, since "Hidden" doesn't do what I thought, I have no need for hidden tasks. Since this is an Admin only tool, and since Admins don't generally like to have things hidden from them, what purpose does a hidden task serve? Sep 21, 2012 at 19:19
  • 3
    Who knows, certainly I have never used that option. Your question was the first time I looked it up to see what it did. :) BTW, instead of changing the user to SYSTEM, does selecting "Run whether user is logged on or not" solve the issue?
    – Karan
    Sep 21, 2012 at 19:25
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?

4
  • Read my reply above, might explain what it does.
    – Karan
    Sep 21, 2012 at 18:56
  • 2
    This SU answer touches on the idea of running it as a different user to avoid seeing the output as well. I don't think there's any specific reason to run it as "System", you could make a user just for running that task, with the permissions you choose to grant them. Sep 25, 2012 at 3:07
  • But why would the configured user affect the visibility of the window? Sep 25, 2012 at 14:42
  • 1
    Keep in mind that the SYSTEM account does not have access to other users' EFS encrypted files.
    – Zero3
    Dec 17, 2017 at 2:05
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

2
  • 2
    Also, you should use Wscript.exe to run your script, not Cscript. Otherwise, black console window might pop up for a moment.
    – BBK
    Jun 26, 2018 at 22:05
  • Here is another example of VBScript which is working for me: faqforge.com/windows/…
    – BBK
    Jun 26, 2018 at 22:21
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());
        }
    }
}
3
  • 1
    And I assume the magic that makes is hidden specifically is due to the ProcessWindowStyle.Hidden portion specifically, correct? I see something about it here: msdn.microsoft.com/en-us/library/… Jun 20, 2018 at 21:23
  • Yes, that is what hides the window. Without it, the window will pop up.
    – AresAvatar
    Jun 20, 2018 at 21:24
  • 1
    This code as written will work on anything that runs with ShellExecute.
    – AresAvatar
    Jun 20, 2018 at 21:29
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.

2

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

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'""
1
  • While this may answer the question, it would be a better answer if you could provide some explanation why it does so.
    – DavidPostill
    Jun 3, 2016 at 9:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .