183

I'm looking for some way to run a batch file (.bat) without anything visible to the user (no window, no taskbar name, .etc..).

I don't want to use some program to do that, I'm looking for something cleaner. I've found a solution that uses VBScript, but I don't really like using VBS, either.

Hennes
  • 64,768
  • 7
  • 111
  • 168
Moayad Mardini
  • 4,003
  • 7
  • 27
  • 24

20 Answers20

157

Solution 1:

Save this one line of text as file invisible.vbs:

CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False

To run any program or batch file invisibly, use it like this:

wscript.exe "C:\Wherever\invisible.vbs" "C:\Some Other Place\MyBatchFile.bat"

To also be able to pass-on/relay a list of arguments use only two double quotes

CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False

Example: Invisible.vbs "Kill.vbs ME.exe"

Solution 2:

Use a command line tool to silently launch a process : Quiet, hidecon or hideexec.

endolith
  • 7,507
  • 25
  • 84
  • 121
harrymc
  • 455,459
  • 31
  • 526
  • 924
  • 2
    Why 4 double quotes, could you please explain? – Starx Jul 28 '11 at 07:44
  • What if we needed to start a program hidden? – Starx Jul 28 '11 at 09:57
  • 6
    @Starx VB syntax. `&` means concatenate strings. Strings are enclosed in double quotes (`"`). Consecutive double quotes (`""`) inside string delimiters means a literal double quote (`"`). So, four double quotes (`""""`) would be a string literal of one character, a double quote (`"`). Put it together, and `"""" & WScript.Arguments(0) & """"` would become `""`, where `` is the first command line argument passed to the script. – Bob Dec 17 '12 at 17:39
  • Cool and simple solution. Checked it, it works. Thanx. – nightcoder Dec 23 '12 at 16:08
  • 1
    Finally, after hours of wasted time searching and trying, this one liner VBS does the trick. Simplest solution for starting batch file silently from scheduled task, thank-you. – Mr. Bungle May 12 '13 at 20:46
  • `CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False` is it possible to get this code in `JAVA ` or atleat in Windows bat file since I am not supposed to use VB..please help .. – Rookie007 Dec 22 '14 at 06:10
  • 7
    How to modify it so it passes all arguments? It would be more convenient to pass the arguments without quoting them. – Ctrl-C Jun 12 '15 at 14:13
  • 3
    Doesn't this still require you to create a batch file for putting the `wscript.exe "C:\Wherever\invisible.vbs" ...` inside of? – endolith Jul 27 '17 at 13:52
  • `hideexec` seems to have some difficulties to run elevated commands. – Vlastimil Ovčáčík Sep 08 '17 at 20:26
  • 1
    The call without parameters works, the unspaced call with parameters works, but I can't find a way of getting the latter to use a script that itself contains spaces and therefore needs to be quoted. Is there a way to call the script passing in `"C:\Some Other Place\MyBatchFile.bat" Param1 Param2`? // EDIT: One workaround is to use an unspaced link to the spaced script. – Lilienthal Sep 12 '17 at 08:46
  • 2
    How to use this solution: **1:** create the batch file you want to run (ie at `C:\apps\mybatch.bat`). **2:** create the invisible.vbs script (ie at `C:\apps\invisible.vbs`). **3:** create a new shortcut by right clicking on the *background* (empty space) of the folder where you want to save the shortcut, mouse over `New` in the context menu, click `Shortcut`. For the "Location of the item" enter `wscript.exe "C:\apps\invisible.vbs" "C:\apps\mybatch.bat"`. Double clicking this shortcut will launch the batch file silently. – James Aug 14 '18 at 20:37
  • This is the one that actually works. I tried Quiet and nircmd, but they actually flicker a cmd terminal when they run, so they don't accomplish the one thing they are supposed to. – Moss May 05 '19 at 12:20
  • 1
    `invisible.vbs` file is deleted by antivirus software. may be we need some other way here. – ajayramesh Aug 09 '19 at 19:59
33

To Hide batch files or command files or any files.... Use Windows XP built in IExpress.exe utility to build a .EXE out of the batch file. When using IExpress make sure you check the run hidden option and check mark all the boxes about not showing anything. After you create your .exe place it in whatever run command folder you choose and you will never see it come up.

Danilo Roascio
  • 2,814
  • 1
  • 12
  • 9
Paul Cheselske
  • 339
  • 3
  • 2
14

I don't like the VBScript solution.

Download and copy nircmd.exe to your %systemroot%\system32 folder, then add this command to first line of your batch:

nircmd.exe win hide ititle "cmd.exe"

You can also change the title of your batch file terminal window by title command to avoid from hiding all cmd windows, like this:

title MyBatch
nircmd.exe win hide ititle "MyBatch"
BijaN-R
  • 141
  • 1
  • 3
  • I use this exact method, but failed for some time that nircmd don't hide the cmd window, just minimized into taskbar, and cannot restored by click on it. Don't know why... – James Yang Jun 21 '18 at 01:42
  • `nircmd.exe execmd` might also be of interest, it solved my use case. It starts a command already hidden. – Rafał G. Nov 15 '21 at 14:47
12

Run the script via an at job without making it interactive:

at 11:00 script.bat

Another solution, if you don't mind installing something like Python, you could simply create a script and run it with pythonw (the linked version for GUI operations). Since you aren't using any graphical APIs, the window will not show. Simply use calls to os.system() and it will mimic a batch script, as it is the same as typing the strings into the command line.

Example:

import os

os.system("tasklist > C:\tasks.txt")
os.system("ipconfig /all > C:\netinfo.log")
John T
  • 163,373
  • 27
  • 341
  • 348
11

If your Batch File can fit into one line and so many characters, then just use a Shortcut by right clicking inside a folder put in a random directory, and skip through the rest of the wizard, and lastly right click on the shortcut just created and click properties and under target, input what you would usually put in a bat file. And Bobs your uncle!

Pros:

 No Command Window
 Ability to use an icon
 Same Functionality of bat file

Cons:

 Limited to one line, so many characters
Jack S
  • 111
  • 1
  • 2
  • The only problem is that you can't associate a filetype with a shortcut. So if you want to open a with a batch script by double-clicking on it in explorer you still need a .bat-file. – Björn Lindqvist Jan 24 '14 at 15:40
  • I'm still trying to find Uncle Bob, is here around anywhere!! – Vomit IT - Chunky Mess Style Jun 15 '17 at 20:13
  • @BjörnLindqvist For this use case i would create a starter pyw script and then assign the file type to the script. The script contents would be e.g. `import subprocess, sys; subprocess.Popen(['path/to/prog.exe', sys.argv[1]], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True, creationflags=subprocess.DETACHED_PROCESS)` and the assignation to the script would be `"path\to\pythonw.exe" "path\to\starter_script.py" "%1"`. I found [RapidEE](https://www.rapidee.com/en/about) to be a pleasant tool to assign filetypes. See also John T's answer for Python v2 ways to run programs. – Nils Lindemann Aug 14 '20 at 18:20
  • btw, one of my personal script tool functions has the signature `doubleclick(*args)`, which does the above. When args is just `[filepath]` it opens `filepath` with its default program. – Nils Lindemann Aug 14 '20 at 18:30
  • I made a mistake in my first comment, i meant [FileTypesMan](https://www.nirsoft.net/utils/file_types_manager.html), not RapidEE (which is also very handy, it edits environment variables). – Nils Lindemann Aug 14 '20 at 18:46
11

use Cmdow is a Win32 commandline utility for NT4/2000/XP/2003 that allows windows to be listed, moved, resized, renamed, hidden/unhidden, disabled/enabled, minimized, maximized, restored, activated/inactivated, closed, killed and more.

Run a batch file hidden passing it parameters:-

cmdow /run /hid mybat arg1 "arg 2"

Chan Tzish
  • 105
  • 3
joe
  • 12,279
  • 12
  • 67
  • 104
  • 10
    cmdow is detected as a malware by most antivirus products. Hence it would be difficult to use this solution in places other than you own PC. – Ganesh R. Oct 29 '09 at 13:02
  • Is it open source? There's no point in using a third party app that's not open source and could be packing trojans when you could do the same thing with by running harrymc's 1 line vbscript (see he answer at the top of the thread). – thebunnyrules Nov 16 '17 at 13:21
  • 2
    Well, it is open source, so that's pretty awesome. Can get the source code here: https://github.com/ritchielawrence/cmdow – thebunnyrules Nov 16 '17 at 13:40
  • @thebunnyrules Is there anywhere that has a compiled version of it? The source is useless to me without a C++ compiler. And I really don't want to download and install a C++ compiler for a single app lol. – corsiKa Nov 27 '17 at 23:13
8

You can also create an AutoHotkey script:

#NoTrayIcon ; To prevent momentary icon pop-up
run whatever.bat arg1 arg2,,hide 
endolith
  • 7,507
  • 25
  • 84
  • 121
8

You can run it minimized easily.

start /MIN batch.cmd
Brad Gilbert
  • 789
  • 1
  • 6
  • 15
6

Using Windows7, you can use the "Scheduled Task" control panel to schedule a batch file to run in the background invisibly with specified permissions and even as the "SYSTEM" account. See screenshot below:

enter image description here

Furthermore, although with not as many capabilities, you can also create a invisible batch script task like this from the command line using the "AT.exe" command without the '/interactive' option.

djangofan
  • 2,851
  • 9
  • 34
  • 35
  • 8
    Hidden checkbox just hides the task from the Task Scheduler user interface ( – Sourav Ghosh Oct 25 '16 at 17:24
  • 2
    Running a task as “SYSTEM” when it doesn’t need “SYSTEM” privileges (and hasn’t been scrutinized for security weaknesses, and hardened) as a bad idea; see [the comments here](https://stackoverflow.com/q/6568736/1672723#6568823). – Scott - Слава Україні Sep 01 '18 at 16:33
  • My understanding is that what makes the task run hidden is the option "Run whether user is logged on or not." Like Sourav Ghosh wrote, the checkbox "Hidden" just keeps Task Scheduler from displaying the task definition. For more information see: https://www.howtogeek.com/tips/how-to-run-a-scheduled-task-without-a-command-window-appearing/ – Dolores Stevens Jul 13 '20 at 11:43
5

The excellent Bat To Exe Converter will convert your .bat or .cmd file to an .exe file.

It has a simple UI. You can choose not to have a console window by selecting an Exe Format of XX Bit | Windows (Invisible) option. Also, it allows you to assign an icon, set a password and several other options.

BullyWiiPlaza
  • 558
  • 2
  • 9
  • 23
5

YET ANOTHER way, from C/C++, is to use the CreateProcess function with the CREATE_NO_WINDOW flag. It has a lot of extra power not shown here. This is just the minimum you need to replace system() or the _spawn() family.

STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);

PROCESS_INFORMATION pi;
ZeroMemory( &pi, sizeof(pi) );

// Spawn creates a cmd.exe window.  CreateProcess doesn't.
//if ( _spawnl( _P_WAIT, szFileNameBAT, szFileNameBAT, NULL ) ) {  
if ( !CreateProcess( szFileNameBAT, szFileNameBAT, NULL, NULL, false,
                     CREATE_NO_WINDOW, NULL, NULL, &si, &pi  ) ) {
  MyErrorFunc( "CreateProcess( \"%s\" ): %d", szFileNameBAT, GetLastError() );
  goto MyErrorHandling;
}

WaitForSingleObject( pi.hProcess, INFINITE );

CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
Swiss Frank
  • 333
  • 1
  • 3
  • 14
  • 2
    Correct answer for WINAPI approach. Here is [source code and application](https://stackoverflow.com/questions/507347/hide-command-window-of-bat-file-that-executes-another-exe-file/56111754#56111754) as addition to your answer. – Alex May 13 '19 at 12:26
4

Despite I've already answered this here's another approach. The different here's that the batch script can self-hide itself during it's run.You'll need two additional scripts though - windowMode.bat and getCMDPid.bat (both are containing .net code). Here how then can be used:

@echo off
call getCmdPid
call windowMode -pid %errorlevel% -mode hidden

echo this should be written into log despite the cmd window is hidden>log
echo ending the log>>log
exit
npocmaka
  • 1,259
  • 12
  • 12
3

Adding my answer here from another post since it was a duplicate and I don't see this solution here so now there are 17 different answers for how you can run a batch file hidden. I will delete my answer on that post at some point in the near future.

Create your batch script with your batch logic and commands in it, and then create a new file with .VBS extension, use the below logic (example I provided below) and just plug in the full path to your .CMD or .BAT file with the PING -t <IPAddress>, etc. commands so it executes the batch in a hidden manner not showing the command window when it is executed.

Where the logic in the example below specifies C:\Folder\BatchFileName.cmd change that to point to the batch script with your commands you are running manually, etc. now.

Save the .VBS file and schedule it with Task Scheduler Problems scheduling a task on windows7 to run on an indefinite schedule for your needs.

Otherwise, just double-click on the .VBS file to run the infinite PING command you put in the batch file you point it to as-needed.

NOTE: I believe this is where I got this script logic from but it works reliably for the same purpose you need here which is to ensure the command window is hidden when a batch file is run Server Fault Source

EXAMPLE VBS SCRIPT RUN COMMAND LINE BATCH HIDING CMD WINDOW

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Folder\BatchFileName.cmd" & Chr(34), 0
Set WinScriptHost = Nothing

EXAMPLE 2

Note: This looks for a bat file with the same name. You can change bat to cmd in script, if needed with this example.

If WScript.Arguments.Count <= 0 Then
    WScript.Quit
End If  

bat = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\")) & WScript.Arguments(0) & ".bat"
arg = ""

If WScript.Arguments.Count > 1 Then
    arg = WScript.Arguments(1)
End If

CreateObject("WScript.Shell").Run """" & bat & """ """ & arg & """", 0, False
Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
3

I did it successfully with nircmd this way:

  1. download nircmd.exe from the link
  2. create your .bat file
  3. the first line of .bat file has to be this: nircmd.exe win hide ititle "cmd.exe"
  4. select both files and create a self extract file using winrar
  5. in "general" tab, check "create autoextract file" box
  6. in "advance" tab select "autoextract" button
  7. there is a new window and select "mode" tab then select option: "hide everything"
  8. select tab "update" and select option "overwrite existing files"
  9. Select "general" tab and define a destination folder for extraction if you wish and in the box "Excecute after extraction" write the full path where your .bat will be extracted. Ex: if you define extraction folder as: "c:\windows\" then the full path to excute after extraction will be: "c:\windows\file.bat" (in the case your .bat is named: file.bat)
  10. click accept on all windows

Now you will have a .exe file that after you doble click on it and install it, your .bat will run totally hidden.

ZEUS
  • 31
  • 1
2

I'm created RunApp to do such a job and also using it in my production env, hope it's helps.

The config like below:

file: config.arg

:style:hidden

MyBatchFile.bat
arg1
arg2

And launch runapp.exe instead.

James Yang
  • 191
  • 1
  • 3
1

I needed to hide the window for a batch file called from an explorer context menu, and also needed to add quotes on the parameter. None of the answers worked for me, so I'm adding my own solution.

I had this on the registry "command" for an explorer context menu:

cmd /c C:\mypath\myprogram.bat "%1"

So to replace it I had to create a vbs like this:

WScript.CreateObject ("WScript.shell").run "cmd /c C:\mypath\myprogram.bat """ & WScript.Arguments(0) & """", 0, False

Save in a place like C:\mypath\myscript.vbs and call it like this on the registry:

wscript "C:\mypath\myscript.vbs" "%1"

Note that the .bat path can't have spaces, so replace it with the 8.3 filename. So for instance C:\Program Files\myprogram.bat have to be referenced as C:\Progra~1\myprogram.bat. To see the 8.3 filename use dir /x

bortao
  • 1,670
  • 4
  • 21
  • 34
1

You do not need to do any special.

If you are running the batch files from a windows service, they should be hidden by default unless you enable the "Allow service to interact with desktop" option on the service.

sgmoore
  • 6,423
  • 2
  • 25
  • 33
0

Using Powershell and Batch files: Wasif's answer was great, but it wasn't well received, and I think that people may not have understood it, so I am expounding on it as the comment I made to his answer wasn't legible on a single line.

Batch file:

PowerShell -NoProfile -ExecutionPolicy Bypass -File ".\pg-start.ps1"

powershell file:

Start-Process cmd -Argument "/c START /B ./pgsql/bin/pg_ctl start -l pgdata/server.log -D pgdata/" -WindowStyle hidden 

This works, Postgres runs in a hidden window.

This fixes the issue I was having where Postgres would die once the batch script completed, which I didn't want.

It may also be related to a terminal bug on Windows 11: https://github.com/microsoft/terminal/issues/14366

  • The Batch file/PowerShell console window will display briefly anyway. While it maybe acceptable for you, I do not think it's what OP asked for. – Martin Prikryl Feb 08 '23 at 06:52
0

This works for windows 8 (possibly others)

  1. Create a batch file as normal
  2. Create a shortcut to the batch file
  3. Right click on the shortcut > properties
  4. Click Layout
  5. Untick "Let system position window"
  6. set left position tp -1000
  7. click apply

  8. Click on the shortcut and it will run but you wont see it :)

Andrew
  • 11
-1

Using Powershell:

Start-Process -FilePath "Batch file path" -ArgumentList "args" -WindowStyle Hidden 
Wasif
  • 7,984
  • 2
  • 19
  • 32
  • 1
    But then you have the PowerShell console window. – Martin Prikryl Sep 17 '20 at 11:27
  • I didn't end up with a powershell window showing at all this way, and the postgres server didn't stop, so it was a happy day for me! Batch file: PowerShell -NoProfile -ExecutionPolicy Bypass -File ".\pg-start.ps1" powershell file: Start-Process cmd -Argument "/c START /B ./pgsql/bin/pg_ctl start -l pgdata/server.log -D pgdata/" -WindowStyle hidden Postgres runs in a hidden window. – Karl Henselin Feb 07 '23 at 17:39