Run a batch file in a completely hidden way

144

85

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.

Moayad Mardini

Posted 2009-10-29T12:42:48.787

Reputation: 3 513

If you're already developing a service, why not use NSSM to create a service out of your compiled file? – KnightOfNi – 2014-10-21T23:52:03.497

Just to add to the list of programs - Hidden Start.

– Karan – 2015-05-06T07:56:53.023

I think your link is down. – Artemis still doesn't trust SE – 2018-05-26T15:05:17.117

"a solution" has link rotted, archive here

– Shaun Dreclin – 2018-09-14T03:12:45.953

Haha! Definitely not. I'm developing a Windows service which will run a batch file every now and then. – Moayad Mardini – 2009-10-29T12:53:34.757

4Windows services don't run as batch files. They run as services. You need to clarify the question--as written and with comments it makes no sense to me. – CarlF – 2009-10-29T14:03:16.293

1I has a Windows service that runs batch files. Not that the service is a batch file. – Moayad Mardini – 2009-10-29T18:37:23.367

1What language are you writing your service in? – Hugh Allen – 2009-10-30T05:53:25.177

there is hstart too. – barlop – 2013-12-10T20:55:18.710

Answers

122

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.

harrymc

Posted 2009-10-29T12:42:48.787

Reputation: 306 093

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 – 2014-12-22T06:10:53.227

4How to modify it so it passes all arguments? It would be more convenient to pass the arguments without quoting them. – Ctrl-C – 2015-06-12T14:13:35.913

3Doesn't this still require you to create a batch file for putting the wscript.exe "C:\Wherever\invisible.vbs" ... inside of? – endolith – 2017-07-27T13:52:50.280

hideexec seems to have some difficulties to run elevated commands. – Vlastimil Ovčáčík – 2017-09-08T20:26:25.903

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 – 2017-09-12T08:46:58.400

1How 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 – 2018-08-14T20:37:30.560

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 – 2019-05-05T12:20:06.237

invisible.vbs file is deleted by antivirus software. may be we need some other way here. – ajayramesh – 2019-08-09T19:59:58.457

2Why 4 double quotes, could you please explain? – Starx – 2011-07-28T07:44:13.640

What if we needed to start a program hidden? – Starx – 2011-07-28T09:57:27.503

5@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 "<argument>", where <argument> is the first command line argument passed to the script. – Bob – 2012-12-17T17:39:34.313

Cool and simple solution. Checked it, it works. Thanx. – nightcoder – 2012-12-23T16:08:04.670

1Finally, 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 – 2013-05-12T20:46:29.620

29

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.

Paul Cheselske

Posted 2009-10-29T12:42:48.787

Reputation: 299

7is there anything equivalent to lexpress.exe in newer windows versions like windows 7? – Sнаđошƒаӽ – 2015-06-23T05:00:16.637

1@Sнаđошƒаӽ It is in windows 7....just tested it – DarthRubik – 2016-07-17T00:12:15.923

@DarthRubik Thanks for sharing that. I too tested it and it works just fine. Time to upvote the answer. :) – Sнаđошƒаӽ – 2016-07-17T05:04:39.447

This does not seem to work on Windows 10 64 since the "Command.com" no longer seems to be present – Arne S – 2017-10-11T23:10:34.750

I can confirm the comment by @ArneS. You can build the EXE but it won't run due to files missing (i.e. Command.com). – Bram Vanroy – 2018-10-23T07:27:13.420

1Oh, good solution actually. – Camilo Martin – 2012-09-08T01:34:20.920

10

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

Posted 2009-10-29T12:42:48.787

Reputation: 101

I'm still trying to find Uncle Bob, is here around anywhere!! – Pimp Juice IT – 2017-06-15T20:13:45.817

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 – 2014-01-24T15:40:38.697

10

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"

joe

Posted 2009-10-29T12:42:48.787

Reputation: 11 615

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 – 2017-11-16T13:21:09.103

2

Well, it is open source, so that's pretty awesome. Can get the source code here: https://github.com/ritchielawrence/cmdow

– thebunnyrules – 2017-11-16T13:40:37.967

@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 – 2017-11-27T23:13:33.000

But how would I run this command? – Moayad Mardini – 2009-10-29T13:01:59.607

9cmdow 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. – 2009-10-29T13:02:20.683

@Ganesh: Any better solution? – Moayad Mardini – 2009-10-29T13:04:08.717

9

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

Posted 2009-10-29T12:42:48.787

Reputation: 149 037

3But how would I run this command? – Moayad Mardini – 2009-10-29T13:07:18.167

You would have to either schedule a scheduled task or write a service. – Unfundednut – 2009-10-29T13:26:43.547

examples added. – John T – 2009-10-29T14:44:38.130

7

You can run it minimized easily.

start /MIN batch.cmd

Brad Gilbert

Posted 2009-10-29T12:42:48.787

Reputation: 771

3or use /B instead! – rany – 2016-06-15T13:26:03.793

start is cmd build-in command. It isn't available from Task Scheduler, etc – gavenkoa – 2020-01-25T19:58:34.360

7

I don't like 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"

or make your batch title custom first with title command to avoid from hiding all cmd windows, like this:

title MyBatch
nircmd.exe win hide ititle "MyBatch"

BijaN-R

Posted 2009-10-29T12:42:48.787

Reputation: 71

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 – 2018-06-21T01:42:47.967

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

Posted 2009-10-29T12:42:48.787

Reputation: 2 459

5Hidden checkbox just hides the task from the Task Scheduler user interface ( – Sourav Ghosh – 2016-10-25T17:24:12.140

1

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.

– Scott – 2018-09-01T16:33:58.603

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

Posted 2009-10-29T12:42:48.787

Reputation: 498

4

You can also create an AutoHotkey script:

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

endolith

Posted 2009-10-29T12:42:48.787

Reputation: 6 626

4

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

Posted 2009-10-29T12:42:48.787

Reputation: 225

2

Correct answer for WINAPI approach. Here is source code and application as addition to your answer.

– Alex – 2019-05-13T12:26:11.427

4

Seems that somebody made a list of the 7-8 ways to run 'silent':

http://forums.techguy.org/dos-other/644932-solved-howto-run-batch-file.html

Rolnik

Posted 2009-10-29T12:42:48.787

Reputation: 1 457

6Please list them here, don't post an answer that's just a link elsewhere. – endolith – 2017-04-16T03:46:57.723

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

Posted 2009-10-29T12:42:48.787

Reputation: 31

2

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

Pimp Juice IT

Posted 2009-10-29T12:42:48.787

Reputation: 29 425

2

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

Posted 2009-10-29T12:42:48.787

Reputation: 887

2

Here I've compiled all ways that I know to start a hidden process with batch without external tools.With a ready to use scripts (some of them pretty rich on options) , and all of them form command line.Where is possible also the PID is returned .Used tools are IEXPRESS,SCHTASKS,WScript.Shell,Win32_Process and JScript.Net

npocmaka

Posted 2009-10-29T12:42:48.787

Reputation: 887

1

cmdow /run /hid "file.extention" arg1 "arg 2"

malak

Posted 2009-10-29T12:42:48.787

Reputation: 29

'cmdow' is not recognized as an internal or external command, operable program or batch file. – Bacco – 2015-11-15T20:08:03.203

3Please include brief explanation of the command. That might help the OP (and others) understand how it works instead of just copying the command and using it. – BloodPhilia – 2011-01-26T20:07:43.787

http://www.commandline.co.uk/cmdow/ – paradroid – 2011-05-25T03:30:48.487

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

Posted 2009-10-29T12:42:48.787

Reputation: 5 961

0

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

Posted 2009-10-29T12:42:48.787

Reputation: 688

0

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

Posted 2009-10-29T12:42:48.787

Reputation: 151

0

I started trying to figure this out and never really succeeded. This was as close as I got:

@echo off
if not defined PIL (
    set PIL=1
    start /min "" %~0
    exit /b
)
TITLE Hello World
echo Hello world!
pause>nul

djangofan

Posted 2009-10-29T12:42:48.787

Reputation: 2 459

0

I had the same problem, but figured it out:
Shell("filehere" & "parametersforbatchfilehere", vbHidden)

Alan Panayotov

Posted 2009-10-29T12:42:48.787

Reputation: 1

-1

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

Posted 2009-10-29T12:42:48.787

Reputation: 1

4Really? This won't show as a button on the taskbar? – Scott – 2014-08-16T00:23:11.243