Run Programs from Task Scheduler Without Showing Console
Table of Contents
Introduction
When you execute a .bat
file or a console application with Task Scheduler, a black window (console) briefly appears.
The true identity of this black window is what is called a console host window.
When Windows launches a console application (a .bat file is executed by the cmd.exe
console application), a console host (conhost.exe
) is started, and this console host displays the black screen (console host window). Console applications perform input/output through the console host window.
Relationship between Console Applications and Console Host
flowchart TD A("Console Application<br> (Process ID: XXXX)") <-->|"Standard Input/Output (stdin/stdout/stderr)"| B("Console Host (conhost.exe Process ID: YYYY)") B <--> |"Console<br>Host Window<br>(Black Window)"| C[User's Screen<br>Keyboard Input]
Attempting to Hide the Console Using PowerShell (Failure)
By specifying the -WindowStyle Hidden
argument in PowerShell, you can instruct the console host to hide the window.
powershell -WindowStyle Hidden -Command "Start-Process [プログラム] -WindowStyle Hidden"
The first -WindowStyle Hidden
launches PowerShell
itself with its window hidden.
The second -WindowStyle Hidden
executes the specified program hidden.
Example: Executing C:\var\ほげ.bat
powershell -WindowStyle Hidden -Command "Start-Process 'C:\var\ほげ.bat' -WindowStyle Hidden"
When this command is executed from an already running command prompt, the command prompt minimizes and execution begins. Clicking the command prompt on the taskbar brings the window back, and no other windows are displayed.
At first glance, it seems to work as expected, but if you execute this command via "Run (Win + R)", a black window flashes momentarily. This is because the console host window is displayed first when the application starts, and then -WindowStyle Hidden
is applied. The same momentary flash of a black window occurs when registered in Task Scheduler.
Solutions
1Using VB Script (WSH)
wscript.exe
is a GUI application, not a console application. Therefore, no console host window is displayed during execution. (cscript.exe is a console application, so a console host window is displayed)
wscript.exe /e:vbscript /b "CreateObject(""WScript.Shell"").Run ""C:\var\ほげ.bat"", 0, False"
/e:vbscript
Executes the code passed as an argument as VB Script/b
Specifies batch mode, which prevents warnings, script errors, or input prompts from being displayed.- CreateObject("WScript.Shell").Run
- 1st argument: Program to execute
- 2nd argument: Window style (0: Hidden)
- 3rd argument: False: Asynchronous execution
When Registering in Task Scheduler
- Program/script
wscript.exe
- Add arguments (optional)
/e:vbscript /b "CreateObject(""WScript.Shell"").Run ""C:\var\ほげ.bat"", 0, False"
2Select "Run whether user is logged on or not"
In Task Scheduler, go to the task's properties > General tab, and select Run whether user is logged on or not
. Selecting this option causes the task to run in non-interactive mode. In non-interactive mode, the console host window is not displayed.
Figure 1. Task Scheduler
- Notes on Non-Interactive Mode
- It is disconnected from the user's desktop.
- GUI applications like Excel may not be able to run.
- Network drives specified by drive letters are not visible (UNC paths should be visible).
- The user executing the task needs "Log on as a batch job" rights.