Rob van der Woude's Scripting Pages

Hide the Console in Batch Files

If we run a batch file by starting it manually, we usually don't mind seeing the "black square" of the console window.
If, on the other hand, we run a batch file scheduled, or remotely, we often would prefer not to see the "black square" at all.

Several tools and techniques exist to hide a console window.

Start a batch file minimized

The most simple solution is to run the batch file minimized.
The batch file will still be visible in the task bar while running.
For batch file started by a shortcut, this may be "sufficiently hidden".

START /MIN "your title" "d:\path\yourbatch.bat"

See this JSIFaq tip on how to make a batch file start another batch file hidden (uses a temporary VBScript file).
Note that, because the script is created and called by a batch file, there will still be a visible console window.

Or download and use my RunNHide.vbs or RunNHide.exe.

 

Make a batch file minimize its own console

Use one of the following commands within a batch file to minimize that batch file's console window:

SETCONSOLE /minimize

or:

TITLE MinimizeMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "MinimizeMePlease"') DO CMDOW %%A /MIN

 

Make a batch file hide its own console

Use one of the following commands within a batch file to hide that batch file's console window:

CONSOLESTATE /hide

or:

SETCONSOLE /hide

or:

TITLE HideMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "HideMePlease"') DO CMDOW %%A /HID

or, using KiXtart:

> Temp.kix ECHO SetConsoleˆ("Hide"ˆ)
KIX32.EXE Temp.kix
DEL Temp.kix

Spoiler alert: completely hiding the console is not possible in the standard command interpreters CMD.EXE or COMMAND.COM.
All techniques to hide the console that have to be called from within the batch file will always show at least a short "black flash" of the console being opened before it is hidden.
Therefore, the "cloaking" of the console has to be started before starting the batch file, which means using an alternative scripting language to start the command interpreter hidden.

To restore console visibility, use:

CONSOLESTATE /show

or:

SETCONSOLE /show

or:

TITLE HideMePlease
FOR /F %%A IN ('CMDOW ˆ| FIND "HideMePlease"') DO CMDOW %%A /VIS

or, using KiXtart:

> Temp.kix ECHO SetConsoleˆ("Show"ˆ)
KIX32.EXE Temp.kix
DEL Temp.kix

 

Start a batch file minimized

If a console window is visible already, but you want to start another batch file minimized, use one of the following commands:

START /MIN d:\path\yourbatch.bat

or:

CMDOW.EXE /RUN /MIN d:\path\yourbatch.bat

 

Start a batch file hidden

If a console window is visible already, but you want to start another batch file hidden, use one of the following commands:

PSEXEC -d CMD.EXE /C d:\path\yourbatch.bat

or:

CMDOW.EXE /RUN /HID d:\path\yourbatch.bat

 

Run a batch file completely hidden

To completely hide the console, a Windows (GUI) executable or an alternative scripting language with GUI based interpreter (e.g. VBScript with WSCRIPT.EXE) has to be used.
Do not use the following commands in a batch file, as this batch file will run in a (visible) console window itself.

RUNNHIDE.EXE d:\path\yourbatch.bat

or:

WSCRIPT.EXE RunNHide.vbs d:\path\yourbatch.bat

or:

HSTART.EXE /NOCONSOLE "d:\some dir\yourbatch.bat"

Make sure the batch file closes its own window in all circumstances, because you won't be able to see whether it does close or keep running "forever".

 

CONSOLESTATE.EXE, RUNNHIDE.EXE and RUNNHIDE.VBS are "third party" tools written by yours truly.
SETCONSOLE, CMDOW, HSTART and PSEXEC are "third party" tools written by other authors.
KiXtart can be downloaded from kixtart.org
VBScript is native and installed in Windows since Windows 2000.

 

Note: CMDOW, HSTART and PSEXEC may sometimes be (wrongly) accused of being malware by some Anti-Virus and malware scanners.
They are not, but they certainly could be abused for "less honorouble" purposes.

page last uploaded: 2016-09-19, 14:56