Run a .bat file in a scheduled task without a window
Solution 1
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).
Solution 2
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
Solution 3
Simply configure the Scheduled Task as "Run whether user is logged on or not".
Solution 4
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.
Solution 5
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
Related videos on Youtube
cire
Updated on September 17, 2022Comments
-
cire over 1 year
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?
-
Efekt almost 10 yearsI found this one a more preferable answer stackoverflow.com/questions/6568736/…
-
-
Sam Cogan almost 15 years+1, just wrote exactly the same thing
-
splattne almost 15 yearsI saw it before you deleted it. I guess we are both bots. ;-)
-
Tim almost 15 yearsalways 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 :-)
-
splattne almost 15 yearsusername, 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! ;-)
-
mackenir about 14 yearsThanks - this was less hassle for me than the .vbs option.
-
IanVaughan about 13 yearsThis 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 about 13 yearsBoth 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 about 13 yearsI 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.
-
Ryan Stille about 13 yearsI 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!
-
djangofan over 12 yearsWhile 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.
-
Jonesome Reinstate Monica about 12 years+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.
-
Mark Meuer almost 12 yearsTo 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.
-
Otiel over 11 yearsPlus 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...).
-
PROgram52bc almost 10 yearsSo 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...
-
CyanCoding almost 10 yearsthere is still popup console, just flash very quick.
-
Tal over 9 yearsI'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)
-
binki over 8 yearsPerfect! You can even disable credential storage and then this ends up being more secure than having SYSTEM run it!
-
Synetech over 8 yearsBecause
start
is not a program, it is a command. You need to specifycmd
as the program to run and/c start /b <file>
as the argument. However, this is still not going to work because it will still create a console window forcmd
and flash a black window on screen. -
ddzzbbwwmm about 8 yearsis
Chr(34)
necessary? -
splattne about 8 years@Lee I guess double quotes are necessary if your path contains spaces.
-
Jan over 7 yearsThis doesn't work on Windows 10 x64, this gives a popup "How do you want to open this file?"
-
Nathan24 over 7 yearsI knew I had done something like this before, but couldn't remember how I did it. Works like a charm.
-
JonathanDavidArndt almost 7 yearsCan 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.
-
Sergei over 6 yearsWorks perfectly when you need to run the task in the context of the logged in user ("Trigger: on connect to user session", "Run task as: Users")
-
JonnyRaa over 6 yearsthis isn't a built in tool
-
Sam Hobbs about 6 yearsSee 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.
-
splattne over 5 years@William This could help: stackoverflow.com/questions/2806713/…
-
GT. about 4 yearsProbably 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) -
Kungfauxn00b almost 4 years@GT. It certainly doesn't go without saying! This was key to making mine work – thanks!
-
Jon over 3 yearsJust 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 over 3 yearsWon't be able to set this unless you are running task scheduler with elevated permissions
-
Jon over 3 years@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.
-
Vojtěch Dohnal about 3 yearsFunny, FortiNet classifies this as a harmful hiddenware. Fake alert most probably.
-
Omkar Dixit about 3 yearsUnder 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, "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 or schtasks worked great.