36

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?

  • possible duplicate of Stop taskeng.exe window from popping up – Ƭᴇcʜιᴇ007 Sep 22 '12 at 17:48
  • 2
    The linked question doesn't mention the Hidden setting. – bwDraco Sep 22 '12 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?". – Ƭᴇcʜιᴇ007 Sep 23 '12 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? – Joshua Frank Sep 24 '12 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. :) – Ƭᴇcʜιᴇ007 Sep 25 '12 at 3:03
45

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

  • 4
    As simple as that. This answer should be the accepted one. – rolve Dec 31 '13 at 11:56
  • Thank you. I had the opposite problem. I wanted the console to display, but it wasn't! – Randy Stegbauer Dec 11 '14 at 21:44
  • 11
    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). – Philipp Munin Feb 10 '16 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 at 15:15
20

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?

  • 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. – Joshua Frank Sep 21 '12 at 18:58
  • 2
    Do you have Show Hidden Tasks checked in the View menu? – Karan Sep 21 '12 at 19:02
  • 1
    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? – Joshua Frank Sep 21 '12 at 19:19
  • 2
    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 '12 at 19:25
18

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?

  • Read my reply above, might explain what it does. – Karan Sep 21 '12 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. – Ƭᴇcʜιᴇ007 Sep 25 '12 at 3:07
  • But why would the configured user affect the visibility of the window? – Joshua Frank Sep 25 '12 at 14:42
  • 1
    Keep in mind that the SYSTEM account does not have access to other users' EFS encrypted files. – Zero3 Dec 17 '17 at 2:05
10

THE ULTIMATE SOLUTION

  • 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

  • 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 '18 at 22:05
  • Here is another example of VBScript which is working for me: faqforge.com/windows/… – BBK Jun 26 '18 at 22:21
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.

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.

1

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());
        }
    }
}
  • 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/… – Pimp Juice IT Jun 20 '18 at 21:23
  • Yes, that is what hides the window. Without it, the window will pop up. – AresAvatar Jun 20 '18 at 21:24
  • 1
    This code as written will work on anything that runs with ShellExecute. – AresAvatar Jun 20 '18 at 21:29
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'""
  • 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 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.