How to run a batch file without launching a "command window"?
Solution 1
Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The second parameter of oShell.Run
is intWindowStyle
value indicating the appearance of the program's window and zero value is for hidden window.
The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx
Solution 2
This is just a simplification of Shaji's answer. You can run your batch script through a VBScript (.vbs) script like this:
HideBat.vbs
CreateObject("Wscript.Shell").Run "your_batch_file.bat", 0, True
This will execute your batch file with no command window shown.
Solution 3
Just to expand on the "Use Windows Scripting" answers (which I consider best because it's built-in already) here's how to do it by using a single wrapper script and passing the name of the "real" batch file as a parameter. Additional parameters will be passed on to the batch file.
If WScript.Arguments.Count >= 1 Then
ReDim arr(WScript.Arguments.Count-1)
For i = 0 To WScript.Arguments.Count-1
Arg = WScript.Arguments(i)
If InStr(Arg, " ") > 0 Then Arg = """" & Arg & """"
arr(i) = Arg
Next
RunCmd = Join(arr)
CreateObject("Wscript.Shell").Run RunCmd, 0, True
End If
So e.g. save the above file as NoShell.vbs
somewhere then call:
NoShell.vbs c:\foo\my_batch_file.bat
Finally, if you're looking to run this from somewhere that doesn't understand the .vbs file (such as an "External Tools" in Visual Studio), you'll want to call C:\Windows\System32\wscript.exe
with the vbs file as its first parameter and your batch file as the second.
Solution 4
Use start with the '/B' option. For example:
@echo off
start /B go.bat
Solution 5
You can change the properties of the shortcut to run minimized.
To run it completely invisibly you'll need something else, like Windows Scripting.
Related videos on Youtube
Jaksa
Updated on September 17, 2022Comments
-
Jaksa over 1 year
On Windows XP, can I run a batch (.bat or .cmd) file, via a shortcut, without a "black window"?
-
Eric about 14 yearsAre you asking if you can prevent the command window from showing up when you run a bat file?
-
Tobias J over 10 yearsYes, you can easily change the default color of the output window by using the Properties on a cmd.exe shortcut. (yes this is a joke...)
-
-
Joey about 14 years
start /b
will just run the program in the currently-allocated console instead of spawning a new one. You'll get a new one anyway since the batch has to run with cmd (which, in turn [surprise], opens a console). -
martineau about 12 yearsYour first suggestion is the way I've always done it.
-
Rolo over 9 yearsFor those that dislike the idea & concept of having to create a new vbs file for every new bat file one creates, as one must using either of the top two answers, this is the perfect solution. Instead of multiple vbs files, you'll have multiple shortcuts. For every new bat file, create a shortcut of the vbs file, right click on it, choose properties > Shortcuts tab, & in the Target box enter the path of the bat file after the path that's already there, just as shown above or like this if the shortcut isn't in the same place as the vbs file:
"C:\My Files\HideCmd.vbs" c:\foo\my_batch_file.bat
. -
Rolo over 9 yearsIf the file path of the bat file has a space, how can it be called via a shortcut to the vbs file? For example:
"C:\My Files\HideCmd.vbs" c:\my things\my_batch_file.bat
. No matter what I try for the path--double quotes around it all, %20 in place of the space, or a combination of those two, nothing works. I get an error message telling me that the file cannot be found or nothing happens. Is this due to user error on my end, a Windows limitation with the Target field for shortcuts, or a problem with the vbs script in which it doesn't handle file paths with spaces properly? -
nixda over 9 yearsThis is not a solution for the OP's problem. There's a difference between Minimizing and Hiding
-
Tobias J over 9 years@Rolo I've updated the script so that if any of the incoming parameters have spaces, it will surround them with quotes. So you'll want to wrap the path to the batch file with quotes when calling it, then it will be considered a single argument and re-wrap before being called.
-
Whoever over 9 yearsIf you change shortcut to use the right icon, the minimized splash on the task bar is actually a nice reminder something is starting.
-
Tomáš Zato - Reinstate Monica over 8 years@Joey yes, but this is very useful for launching batch files from other applications through shell execution (like
SYSTEM
in C/C++). -
Joey over 8 years@TomášZato: You can just call the batch file directly in that case, or, if whatever you use to spawn a process doesn't create a shell first, use
cmd /c foo.cmd
. There's absolutely no need to usestart
in those scenarios, and, I'd argue thatsystem
should not be used in favor ofCreateProcess
. By launching a batch file, you're dependent on one platform already, you might as well use the better tools for the job. -
Dan Z over 7 yearsWould the third line be: strArgs = "cmd /c FILEPATH\testing.bat"?
-
T_D about 7 years@Joey Thanks a bunch, never heard of
cmd /c
before. The cmd command is what I needed in my case, but all the stackexchange answers I found so far, about a batch file running another batch file, have top answers to use start... -
Laurens Swart about 7 yearsAny way to run the bat in elevated mode while still remaining invisible?
-
Guru Josh almost 7 yearsThis is a very nicely designed and useful application.
-
Eryk Sun almost 7 years
start /b go.bat
will not allocate a new console (i.e. an instance of conhost.exe -- or a thread in csrss.exe before Windows 7). However it will make a mess since the batch script is run with a new instance of cmd.exe that's attached to the same console. Both instances of the shell will compete to read input from the console, typically alternating. It needs/w
(e.g.start /b /w go.bat
) in order to wait on the second instance, which is similar to usingcmd /c
if usingsystem
. Also, if you're callingCreateProcess
, rungo.bat
directly. There's no need forcmd /c
. -
dmitry_romanov almost 6 yearsThat is a nice and clean solution for my problem! Thank you!
-
Mehdi Dehghani almost 6 yearsHow to run
.bat
file as administartor using this method? -
Mehdi Dehghani almost 6 yearsHow to run
.bat
file as administrator using this method? -
gumuruh over 5 yearsminimizing... hehe.
-
Mister SirCode almost 5 years@MehdiDehghani properties > security > run as admin. If that only works for exe or apps, then oh wells.
-
Fabian Röling over 3 yearsThis was the only thing that worked for me. I wanted to use this script to maximise another window, but whenever I followed the usual instructions for hiding task windows, like making a shortcut with "minimised" selected, using the "hidden" option and "whether logged in or not", etc., I always either still got a window or the script didn't work, probably because it no longer ran as the correct user.
-
lyrica almost 3 years@Whoever Can you clarify what you mean?