169

On Windows XP, can I run a batch (.bat or .cmd) file, via a shortcut, without a "black window"?

3
  • 6
    Are you asking if you can prevent the command window from showing up when you run a bat file?
    – Eric
    May 11, 2010 at 17:36
  • 8
    Yes, 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...)
    – Tobias J
    Jan 10, 2014 at 17:45
  • I hope the OP didn't take this joke the wrong way. Usually, when one is searching online for computer solutions, one isn't in a particularly jocular mood. So when I came across this in the course of my troubleshooting, it provided much-welcome comical relief, if only briefly. Hooda Thunk that you're humour would be helping others a better part of a decade later. Sep 1, 2023 at 22:01

9 Answers 9

109

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

4
  • Would the third line be: strArgs = "cmd /c FILEPATH\testing.bat"?
    – Dan Z
    Nov 7, 2016 at 3:46
  • 1
    Any way to run the bat in elevated mode while still remaining invisible? May 9, 2017 at 22:30
  • How to run .bat file as administartor using this method? Jul 27, 2018 at 5:14
  • This 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. Aug 28, 2020 at 16:54
56

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.

2
  • How to run .bat file as administrator using this method? Jul 27, 2018 at 5:21
  • @MehdiDehghani properties > security > run as admin. If that only works for exe or apps, then oh wells. Jun 24, 2019 at 9:32
29

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.

3
  • 8
    For 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
    Sep 10, 2014 at 13:53
  • If 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?
    – Rolo
    Sep 10, 2014 at 13:59
  • @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.
    – Tobias J
    Jan 12, 2015 at 21:11
18

Use start with the '/B' option. For example:

@echo off
start /B go.bat
5
  • 32
    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).
    – Joey
    May 11, 2010 at 18:02
  • @Joey yes, but this is very useful for launching batch files from other applications through shell execution (like SYSTEM in C/C++). Jan 10, 2016 at 6:10
  • 1
    @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 use start in those scenarios, and, I'd argue that system should not be used in favor of CreateProcess. By launching a batch file, you're dependent on one platform already, you might as well use the better tools for the job.
    – Joey
    Jan 10, 2016 at 10:57
  • @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...
    – T_D
    May 5, 2017 at 14:58
  • 2
    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 using cmd /c if using system. Also, if you're calling CreateProcess, run go.bat directly. There's no need for cmd /c.
    – Eryk Sun
    Jul 17, 2017 at 16:05
12

You can change the properties of the shortcut to run minimized.

To run it completely invisibly you'll need something else, like Windows Scripting.

1
  • 1
    Your first suggestion is the way I've always done it.
    – martineau
    Mar 19, 2012 at 23:32
11

Simple solution, without using any extra programs.

  1. Create the batch file you want to execute and test it.
  2. Create a shortcut for it.
  3. Edit the properties of the shortcut: in the Shortcut tab, choose Run Minimized. Assign a hot key to it and you're done!

Good luck!

5
  • 18
    This is not a solution for the OP's problem. There's a difference between Minimizing and Hiding
    – nixda
    Oct 27, 2014 at 9:10
  • If you change shortcut to use the right icon, the minimized splash on the task bar is actually a nice reminder something is starting.
    – Whoever
    Jan 24, 2015 at 18:55
  • That is a nice and clean solution for my problem! Thank you! Jun 17, 2018 at 4:37
  • minimizing... hehe.
    – gumuruh
    Aug 17, 2018 at 4:06
  • @Whoever Can you clarify what you mean? Jul 5, 2021 at 8:47
7

Free GPL open source "Create Hidden Process"

http://www.commandline.co.uk/chp/

Microsoft Security Essentials, and probably most other virus/malware scanners will treat the executable, chp.exe, as a virus because it hides whatever program you specify from displaying a window or a task bar button, just like viruses do.

It's not a virus. It doesn't hide the target process from appearing in task manager for example. And of course the source code is included so you can see that it's very small and doesn't do anything but run whatever program you give it.

You don't even have to trust that the included chp.exe really was built from that source. You can go ahead and discard the included chp.exe and compile your own from the source, and all the necessary tools to do so are even free and downloadable.


You can also just make a shortcut to the .bat or .cmd file, then right-click on the shortcut, Properties, Shortcut tab, Run: Minimized. Then in scheduled tasks, use the shortcut instead of the .bat/.cmd file directly. That will prevent a window from popping up, but a taskbar button will still appear.

1
  • This answer is obsolete.
    – Akito
    Mar 20, 2023 at 12:54
7

You can use window scripting such as AutoIt.

As an example, just write this to the AutoIt script editor. It's fairly simple

Run("C:\test\batchfile.bat", "", @SW_HIDE)

If you want to run it in a loop,

$x=0
Do
 Run("C:\test\batchfile.bat", "", @SW_HIDE)
 Sleep(5000)
Until $x = 1

Compile it as .exe - and you are done.


Likewise, in AutoHotkey:

#NoTrayIcon ; To prevent momentary icon pop-up
run whatever.bat arg1 arg2,,hide 
6

Use Hidden Start (costs $20)

Hidden Start - Run Applications and Batch Files without a Console Window or UAC Prompt

Console applications and batch files are regularly run at Windows startup or in a schedule. The main inconvenience of this is that each application opens a console window that flickers on the screen. Hidden Start (or Hstart) is a lightweight command line utility that allows you to run console applications and batch files without any window in the background, handle UAC privilege elevation under Windows 7 and Vista, start multiple commands in parallel or synchronously, and much more.

enter image description here

1
  • 1
    This is a very nicely designed and useful application.
    – Guru Josh
    May 18, 2017 at 3:40

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