Windows – How to run batch file without a window in ONE file

batch-filecommand-line-interfacescriptingwindowswindows-command-prompt

I know how to run batch files without a window thanks to this question here: Run a .bat file in a scheduled task without a window.

The first answer is the one I am using. It states that to run a batch file without a window, create the following vbs file and run the vbs file instead.

Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing

My question is whether or not it is possible to do this in just one single file. (somehow combining the vbs script and a batch file script) The answer above would require two files as the vbs file refers to a batch file. If it is possible, how would it be done?

Best Answer

In the script, create a temporary file and write the batch file into it. Then execute the script from the temporary file. Delete the temporary file when done.

Ideally, write the temporary file to a location where other users can't access it because you have a potential race condition there that could be used to elevate permissions.