164

I have a scheduled task that starts a batch script that runs robocopy every hour. Every time it runs a window pops up on the desktop with robocopy's output, which I don't really want to see.

I managed to make the window appear minimized by making the scheduled job run

cmd /c start /min mybat.bat

but that gives me a new command window every hour. I was surprised by this, given cmd /c "Carries out the command specified by string and then terminates" - I must have misunderstood the docs.

Is there a way to run a batch script without it popping up a cmd window?

splattne
  • 28,348
  • 19
  • 97
  • 147
Tom Dunham
  • 1,785
  • 2
  • 13
  • 8
  • 1
    I found this one a more preferable answer http://stackoverflow.com/questions/6568736/how-do-i-set-a-windows-scheduled-task-to-run-in-the-background – Efekt May 10 '14 at 05:46

11 Answers11

162

You could run it silently using a Windows Script file instead. The Run Method allows you running a script in invisible mode. Create a .vbs file like this one

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing

and schedule it. The second argument in this example sets the window style. 0 means "hide the window."

Complete syntax of the Run method:

 object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

Arguments:

  • object: WshShell object.
  • strCommand: String value indicating the command line you want to run. You must include any parameters you want to pass to the executable file.
  • intWindowStyle: Optional. Integer value indicating the appearance of the program's window. Note that not all programs make use of this information.
  • bWaitOnReturn: Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).
Tobias Kienzler
  • 388
  • 1
  • 9
  • 28
splattne
  • 28,348
  • 19
  • 97
  • 147
  • +1, just wrote exactly the same thing – Sam Cogan May 17 '09 at 09:10
  • I saw it before you deleted it. I guess we are both bots. ;-) – splattne May 17 '09 at 09:14
  • always with the great answers! i was about to write that you deserve to become the Jon Skeet of serverfault... then i realized you already have :-) – username May 17 '09 at 13:04
  • 1
    username, I honestly think that Sam deserves it much more than me. But thank you anyway! And don't mention me and Jon Skeet in the same sentence. That's blasphemy! ;-) – splattne May 17 '09 at 14:00
  • While I can see why this answer is the best in the thread, I don't see why so many upvotes when it doesn't even answer the question directly. It answers the question indirectly. – djangofan Nov 14 '11 at 20:04
  • 3
    To the question "Is there a way to run a batch script without it popping up a cmd window?", it gives a very direct answer: Run it using a Windows Script file. – Mark Meuer Aug 02 '12 at 15:51
  • So question: how to run with args? E.g., "C:\batch.bat %1" works with the cmd window popping up, but inserting the same thing into this script file gives an error dialog... – Joe May 09 '14 at 14:19
  • I've used your code, I've confirmed that the file I run does indeed run, but... the VBS appears to keep running indefinitely. I even set bWaitOnReturn to 0, but it just remains stuck. Any idea why? (The file I run is a PHP script, which works correctly and terminates) – Tal Aug 14 '14 at 15:25
  • is `Chr(34)` necessary? – ddzzbbwwmm Mar 17 '16 at 18:11
  • 2
    @Lee I guess double quotes are necessary if your path contains spaces. – splattne Mar 21 '16 at 13:13
  • I knew I had done something like this before, but couldn't remember how I did it. Works like a charm. – Nathan24 Jan 24 '17 at 02:20
  • How would this be generalized into a program that could be run like `bg.bat "C:\Scheduled Jobs\mybat.bat"` – William Sep 10 '18 at 03:57
  • @William This could help: https://stackoverflow.com/questions/2806713/can-i-pass-an-argument-to-a-vbscript-vbs-file-launched-with-cscript – splattne Sep 10 '18 at 07:04
  • 4
    Probably goes without saying, but in Task Scheduler, run the .vbs file using `wscript.exe` by setting the 'Action' to run *wscript* (usually `%WINDIR%\System32\wcript.exe` )and using the fully-qualified path to the .vbs file as an argument. (Other use cases might use *cscript* - same directory as *wscript*) – GT. Feb 28 '20 at 06:18
  • 1
    @GT. It certainly doesn't go without saying! This was key to making mine work – thanks! – Kungfauxn00b May 18 '20 at 17:46
63

Are you running this as a scheduled task? If so set it to run as a different user account then it won't be visible to the logged on user. If the script needs no network access to items that need windows auth (like file shares or printers), you can run it as "nt authority\system" and leave the password blank. On Windows 7, just set the user to SYSTEM, and press OK.

(You probably have to use a real user though if you're using robocopy...)

JR

John Rennie
  • 7,756
  • 1
  • 22
  • 34
  • 3
    Thanks - this was less hassle for me than the .vbs option. – mackenir Feb 10 '10 at 16:45
  • I like the idea, but I am the only user on this desktop, and its a work PC so cannot create others. When ever I select anything else in the "Run as" box, it asks for a password and confirmation, of which mine fails. – IanVaughan Feb 09 '11 at 15:19
  • 8
    I set the "Run as" user to SYSTEM (which it later changed o NT AUTHORITY\SYSTEM) and it worked for me. I no longer see the popup CMD window when my scheduled task runs. Thanks! – Ryan Stille Mar 25 '11 at 17:28
  • 1
    +1, this is elegant. Be sure to enter "system" as the user name, then win7 does the rest for you. Note that you DO get network access to the internet, just not to network shares and things that need windows auth. – Jonesome Reinstate Monica Mar 02 '12 at 04:36
  • 1
    System user is simple and elegant, great TIP! +1 ! – Matteo Conta Oct 30 '12 at 08:40
  • 2
    See [Implementing Least-Privilege Administrative Models](https://docs.microsoft.com/en-us/windows-server/identity/ad-ds/plan/security-best-practices/implementing-least-privilege-administrative-models). Is use of the System account for this a violation of Least-Privilege policies? If so then administrators should know to **not** do this in most situations. – Sam Hobbs Apr 13 '18 at 16:54
  • Won't be able to set this unless you are running task scheduler with elevated permissions – Jon Feb 04 '21 at 03:52
  • 1
    Under XP, creating a task set to run as SYSTEM or "NT AUTHORITY\SYSTEM" via the Scheduled Tasks GUI appeared to work, but the task would fail to run, returning "Could not start". According to [KB223375](https://web.archive.org/web/20031012020125/http://support.microsoft.com/default.aspx?scid=kb;EN-US;223375), "This issue occurs because you cannot configure a task to use the System account using the Scheduled Tasks Wizard." However, creating the task via [at](https://web.archive.org/web/20080517085709/http://ss64.com/nt/at.html) or [schtasks](https://ss64.com/nt/schtasks.html) worked great. – Miles Wolbe Apr 15 '21 at 07:38
39

Simply configure the Scheduled Task as "Run whether user is logged on or not".

Peter Meinl
  • 491
  • 4
  • 2
  • 9
    Perfect! You can even disable credential storage and then this ends up being more secure than having SYSTEM run it! – binki Aug 19 '15 at 17:50
  • 2
    Just note that it requires "Log on as batch job" rights to be able to do this -- regular user accounts will not have this permission – Jon Feb 04 '21 at 03:49
19

You could also try CHP (Create hidden process), does exactly what you'd think...

CHP.EXE mybat.bat

Runs with no command window. Perfect! Made by the same people as CMDOW, but this is more appropriate.

rocketmonkeys
  • 298
  • 2
  • 4
10

CMDOW is an awsome tool that allows you to do many, many things to windows from the command line.

One of the simplest things to do is hide the current window (usually as a first line in the bat file) with:

cmdow @ /hid

or start a new hidden process with

cmdow /run /hid mybat.bat 
StarGeek
  • 103
  • 2
itsadok
  • 1,839
  • 5
  • 21
  • 33
  • 4
    Both this and Rocketmonkeys suggestion involve downloading new tools, which means more compatibility over various peoples desktops. The baked in, using windows commands is much better. – IanVaughan Feb 09 '11 at 15:14
  • Plus cmdow is detected as "hazardous" by some anti-virus programs (it is **not** hazardous, but the detection can itself cause some problems if the cmdow file is quarantined...). – Otiel Dec 10 '12 at 15:36
  • 3
    there is still popup console, just flash very quick. – Bamboo Jul 22 '14 at 10:37
6

You can create a shortcut to the batch file, set the shortcut to start minimized (in the shortcut's properties, 'Shortcut' tab), and then set the job to start the shortcut.

Important: You'll need to specify the path to the shortcut manually by typing it into the Run text field, complete with the '.lnk' extension; if you just try to browse to it, it will helpfully redirect itself to whatever the shortcut points to.

Doug Kavendek
  • 313
  • 3
  • 10
  • This doesn't work on Windows 10 x64, this gives a popup "How do you want to open this file?" – Jan Dec 13 '16 at 15:22
  • @Jan It _does_ work on Windows 10 x64, you need to make sure you keep your original file in the same directory as the shortcut, or else update the shortcut properties. – Jon Feb 04 '21 at 03:57
4

Try invoking the script with

start /b <command>
  • 2
    This does not work, the Scheduled Task->Status states "Could not start", thats with : start /b C:\file.bat : and : start /b "C:\file.bat" : but : C:\file.bat : works just fine. – IanVaughan Feb 09 '11 at 15:12
  • 1
    Because `start` is not a program, it is a command. You need to specify `cmd` as the program to run and `/c start /b ` as the argument. However, this is still not going to work because it will still create a console window for `cmd` and flash a black window on screen. – Synetech Oct 02 '15 at 20:23
  • 1
    Can also confirm with @Synetech that this will not create a new window, but you still need to have a console window open in order to start it. This is indeed a handy command, but cannot be used as requested with Scheduled Tasks. – JonathanDavidArndt Jul 17 '17 at 12:12
2

I realize this question has already been answered with a perfectly good resolution that is native to Windows and thus should be the most compatible, and I agree completely.

I also wanted to say that I disagree with @splattne's comment (but not his actual answer) -- that the resolution in the other referenced thread deserves the credit. That answer involves running the script as a different user (SYSTEM), which is pretty much the equivalent of giving the script root access. It will also fail for jobs such as ROBOCOPY (as referenced by John Rennie), which require network access.

I have never tried CMDOW before, but I would like to offer another similar resolution, which [although is not natively-installed on Windows] is still highly-portable to most versions, and comes in both 32 and 64-bit versions, and that is NirCmd.

NirCmd is a very powerful tool that has myriads of options, the most useful of which, I personally find to be its ability to launch hidden command windows by simply executing the following:

c:\path\to\nircmd.exe exec hide "c:\path\to\mybat.bat"

From the exec section of The NirCmd Command Reference:

exec [show/hide/min/max] [application + command-line]

Runs an application, and optionally specify one or more command-line parameters for the executed application. The [show/hide/min/max] parameter specifies whether the running application will be visible or not. If 'hide' is specified, the running application won't be visible to the user. If 'max' is specified, the running application window will be maximized. If 'min' is specified, the running application window will be minimized.

EDIT: I was trying to run a ROBOCOPY job and tried the method in this answer, and it did not work, even after editing the network access privileges. I tried double-clicking the script and could not get it to work, but could only get it to run under an elevated command prompt. I did create a shortcut to the batch file and have it run as Administrator and was able to get it execute by double-clicking it, but the method I ended up going with was to run it hidden as SYSTEM (I know, I know) -- but it does work with ROBOCOPY, for what it's worth, as long as the batch file has the correct permissions.

EDIT 2: For some reason, it would not work as SYSTEM (probably the network access thing referenced earlier) -- I only noticed this after actually running ROBOCOPY without the /L flag, which is basically just a simulation and [apparently] doesn't actually connect to the remote system, but when I run the batch file with highest privileges and check the hidden box, and I can still run it as the logged in user in the background without a command window showing, for whatever this is worth to anyone.

rubynorails
  • 369
  • 3
  • 14
0

Another solution I've used is Hidden Start

SteveC
  • 271
  • 3
  • 8
  • 21
-2

Try putting in an exit command at the end of your batch file. This should close the command window when the script is done.

-2

To hide the output (although not the window), add this to the beginning of your batch file:

@echo off
voidstate
  • 249
  • 2
  • 8