Windows XP or later Windows: How can I run a batch file in the background with no window displayed?

128,706

Solution 1

Do you need the second batch file to run asynchronously? Typically one batch file runs another synchronously with the call command, and the second one would share the first one's window.

You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window. If both batch files write to the console simultaneously, the output will be overlapped and probably indecipherable. Also, you'll want to put an exit command at the end of your second batch file, or you'll be within a second cmd shell once everything is done.

Solution 2

Here is a possible solution:

From your first script, call your second script with the following line:

wscript.exe invis.vbs run.bat %*

Actually, you are calling a vbs script with:

  • the [path]\name of your script
  • all the other arguments needed by your script (%*)

Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:

  • intWindowStyle : 0 means "invisible windows"
  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish

Here is invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
        anArg = args.Item(k)
        sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False

Solution 3

Convert the batch file to an exe. Try Bat To Exe Converter or Online Bat To Exe Converter, and choose the option to run it as a ghost application, i.e. no window.

Solution 4

I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps poping up, here is your solution. Use a VBS Script to call the batch file ...

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0 
Set WshShell = Nothing 

Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.

Solution 5

For self-hiding you can use getCmdPID.bat and windowMode.bat:

@echo off

echo --- self hiding bat ----
pause
call getCmdPid.bat
set PID=%errorlevel%
call windowMode.bat -pid %PID% -mode hidden

Here's my collection of ways to achieve that - and even more - where it was possible I've tried to return also the PID of the started process (all linked scripts can be downloaded and saved with whatever name you find convenient):

  1. The IEXPRESS solution can be used even on old win 95/98 machines. Iexpress is a really ancient tool that is still packaged with Windows - as arguments accepts only the command and its arguments.

Example usage:

call IEXPhidden.bat "cmd /c myBat.bat"  "argument"
  1. SCHTASKS - Again accepts only two arguments - the command and the arguments.Also checks if it's started with elevated permissions and if possible gets the PID of the process with WEVTUTIL (available from Vista and above so the newer version of windows will receive the PID) command.

Example usage:

call SCHPhidden.bat "cmd /c myBat.bat"  "argument"
  1. 'WScript.Shell' - the script is full wrapper of 'WScript.Shell' and every possible option can be set through the command line options.It's a jscript/batch hybrid and can be called as a bat.

Example usage (for more info print the help with '-h'):

call ShellRunJS.bat "notepad.exe" -style 0 -wait no 
  1. 'Win32_ProcessStartup' - again full wrapper and all options are accessible through the command line arguments.This time it's WSF/batch hybrid with some Jscript and some VBScript pieces of code - but it returns the PID of the started process.If process is not hidden some options like X/Y coordinates can be used (not applicable for every executable - but for example cmd.exe accepts coordinates).

Example usage (for more info print the help with '-h'):

call win32process.bat "notepad" -arguments "/A openFile.txt"  -showWindows 0 -title "notepad"
  1. The .NET solution . Most of the options of ProcessStartInfo options are used (but at the end I was too tired to include everything):

Example usage (for more info print the help with '-h'):

call ProcessStartJS.bat "notepad" -arguments "/A openFile.txt"  -style Hidden -directory "." -title "notepad" -priority Normal
Share:
128,706
VonC
Author by

VonC

System Configuration Management Administrator (ClearCase, SVN, Git), defining various merge workflows between branches. Development Architect, which involves: tools support around java technologies, including eclipse. code quality evaluation, including metrics definitions, and code static tools for different populations. code management (Jira, FishEye/Crucible, Maven, Hudson, Sonar)

Updated on July 05, 2021

Comments

  • VonC
    VonC almost 3 years

    I know I have already answered a similar question (Running Batch File in background when windows boots up), but this time I need to launch a batch:

    • from another batch,
    • without any console window displayed,
    • with all arguments passed to the invisible batch.

    The first batch is executed in a console window. However, I do not want the second batch (launched by the first in a asynchronous way) to also display a console window.

    I have come up with a VBScript script which does just that, and I put the script as an answer for others to refer to, but if you have other ideas/solutions, feel free to contribute.

    Note: The console window of Windows command processor is named not really correct DOS window by many people.


    Thank you all for the answers. From what I understand, if I need to asynchronously call a script to run in an invisible mode:

    • From a second script already in a console window, start /b is enough.
    • From Windows, without triggering a second window, my solution is still valid.
    • Oddthinking
      Oddthinking over 15 years
      You are launching the batch file from ANOTHER batch file? Does this already running batch file have a window?
    • VonC
      VonC over 15 years
      Yes, this other (first) batch is executed in a DOS windows. However, I do not want the second batch (launch by the first in a asynchronous way) displays also a windows (which would happen with a 'start /b' command)
  • VonC
    VonC over 15 years
    Correct. I believed start /b would open a new windows, but if executed from a DOS windows, it does share the same windows.
  • VonC
    VonC over 15 years
    Interesting, but a tight overkill for this scenario.
  • VonC
    VonC over 15 years
    Possible, but I will try to avoid any extra step in this instance.
  • VonC
    VonC over 15 years
    Possible, but not exactly what I am after (direct asynchronous call in background)
  • Tobias
    Tobias about 12 years
    I'm usually not that paranoid but wouldn't it be very stupid to let someone anonymous generate that exe for me? All kinds of malicious stuff could be injected.
  • VonC
    VonC over 10 years
    Interesting approach, certainly shorter than my solution. +1
  • Patrick Bard
    Patrick Bard over 10 years
    Great solution. Reading this I've notice there is no really need to use Set WshShell = Nothing, worked fine for me and you can have an even smaller way to do it.
  • YetAnotherBot
    YetAnotherBot over 5 years
    The only problem is that this creates a process named "cmd.exe", which is hard to find in the future.
  • VonC
    VonC almost 3 years
    Interesting option, from github.com/imrolii/SiteDownloads. Upvoted. nircmd.exe has just been updated one hour ago. Is it yours? Also, I did not know about unblocked CMD! (imrolii-html.tk/downlist/mysoftware.html)
  • i Mr Oli i
    i Mr Oli i almost 3 years
    Haha, unblocked CMD is a tool I use when services let you use bat scripts but not command prompt. nircmd.exe isn't my tool, you can find the original author by googling it.
  • VonC
    VonC almost 3 years
    Right: nirsoft.net/utils/nircmd.html, from Nir Sofer. However, as said here: "I'd feel differently if Nir Sofer released source."
  • i Mr Oli i
    i Mr Oli i almost 3 years
    I haven't changed anything, just uploaded a direct mirror to my GitHub. :)