Close a Window in Windows Command Prompt - windows

Hey I've got a quick question,
I know that you can start applications minimized through the command prompt with something like:
start /min "" "C:\Windows\notepad.exe"
However is there a way to automatically close the window, now I don't mean kill the process/task. Just close the window. In notepads case this would work the same way as killing the process, but in something that will minimize to a system tray on a window close, like Skype, or Discord for example this wouldn't kill the process but instead just put the task away from sight.
Thanks

This is not possible in batch files alone, instead this script creates a temporary VBScript at %temp%\hidden.vbs and then passes arguments to it using wscript.exe
This script doesn't minimise it or send it to the system tray, it just hides it and is only visible in Task Manager
#echo off
(echo CreateObject^("Wscript.Shell"^).Run """" ^& WScript.Arguments^(0^) ^& """", 0, False) > "%temp%\hidden.vbs"
set launch=wscript.exe "%temp%\hidden.vbs"
%launch% notepad

Related

Hide CMD after opening

Does anybody know how to hide a console window (cmd), by just typing something into the console (not usbing VBS, batch-file or whatever), just using the cmd?
Is there a way to minimize it but still be able to type something?
Also heard of a possibility to just move the window out of your monitors size, does this work and if yes how?
You can start it minimized with start /min or start a new cmd minimized and close the old cmd start /min && exit but you loose the focus, you can't type commands in the minimized cmd, you need to klick it and bring it to front. You could execute a bat file with your commands (to execute them "invisible") start /min cmd.exe /C mybat.bat && exit
For "windows+r" (run) you can use: cmd.exe /C "start /min cmd"

.bat start program and force minimized

I have a tiny .bat file that opens 3 programs
One of them should be started minimized:
START /MIN C:\PROGRA~2\HOBBYI~1\VLCSET~1\VLCSET~1.exe
This program, however, ignores the /MIN tag and it cannot be minimized manually either (only has the exit symbol and ignores win+m).
How can I force it to either hide or run minimized?
I am using win 10
If the program doesn't allow minimization, you can't minimize it. There might be some ugly hacks to work around this, but that's usually the point where you should ask yourself whether you really want to use a program that doesn't seem to fit your requirements.
You have to use the following:
"cmd.exe /c start /min your_file.bat"
If you with the cmd window to close itself once the .bat file has completed, use this:
"cmd.exe /c start /min your_file.bat ^& exit"

Windows 8.1 unable to run batch file without command prompt flashing

I have scheduled a task to run a batch file, and currently every time the task executes, there is a command prompt box that flashes. This is very undesirable. The node.js command prompt window executes minimized correctly, it is the windows command prompt that flashes.
Here is the action that the scheduled task runs:
cmd /min /c "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
and here is the contents of the batch file, (probably unnecessary to show, but I was originally trying to run these commands straight from the task scheduler. Putting them in this batch file instead has got me closest to what I actually want to happen: run a javascript file with node.js minimized):
cd "C:\Program Files (x86)\BuildingIntelligence\javadobe\" & START /MIN node index.js /exit
A list of the commands I've tried:
cmd.exe /c start /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
cmd.exe /c start /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat" & /exit
%comspec% /c start "" /min "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
all of these commands leave the command prompt box open, not closing it but minimizing it after it opens. There is also a notification that says "not enough storage is available to process this command".
I need to run this task with the highest privileges, and while it needs to be invisible most of the time, it needs to be able to show a dialog box occasionally when the task is run.
Thanks in advance for any help!
Basically, if what you have tried doesn't work, you're going to need a third party tool. NirCMD should do it and it's freeware.
NirCMD exec hide "C:\Users\computeruser\Building Intelligence\javadobe\runCheck.bat"
At the bottom of this page there is a link for the download. This page covers what the tool is capable of.

How to close the parent cmd console window from within the script it's hosting?

I am using the following command in the registry to run a vbscript locally from Windows Explorer's context menu:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1""
I am using the /K switch for 2 reasons: to keep the console window open in case of script errors, and to change the back and fore colors of the console (this is actually done with the /T switch, but for some reason that I don't understand, the colors revert to default when the /C switch is used.
My question: how can I close the host console window (attached to the "cmd.exe" process) on normal execution end? WScript.Quit obviously returns to the console prompt. I thought about a WMI query for Win32_Process based on CommandLine, but that doesn't work if the script is invoked multiple times with the same parameter (i.e. file). Is there a way to obtain the top most parent process's id for example, which I can then use to terminate?
I believe you can simply append &&exit to your command string:
cmd /T:1F /K "cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%1"&&exit"
Note that your quoted arguments are not quoted because of the extra enclosing quotes. You might be better off with:
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^&&exit^"
Update
The script engine does not set the exit code upon runtime errors - it leaves that task to the script writer. It does set a positive exit code if there is a syntax error. That is a very unfortunate design.
I think the easiest thing to do is to modify your VBS script to always exit with a negative value upon success: WScript.Quit -1. Then you can conditionally EXIT the console only if the exit code is negative.
cmd /T:1F /K ^"cscript.exe //nologo "C:\Some Path\Some Folder\MyScript.vbs" "%~1"^||if not errorlevel 0 exit^"

How to start a batch file minimized with task scheduler in Windows 8? - %comspec% method not working anymore after Windows 7

After Windows XP, I always use the trick below to start batch files minimized with Windows Task Manager.
From http://www.pcreview.co.uk/forums/running-bat-files-minimized-scheduler-t2125918.html:
"prequisite: all your batch files have an exit-command to finish the actions off. If you do not exit, you will end with a command prompt blinking.
This is what I keep using:
%comspec% /c start /min "C:\Scripts\Destination_inbound_ftp5.bat"
When you save this in the properties, you will get a follow-up dialogue asking you if you meant all this to be parameters or not. Answer NO and the task will be saved as you would expect.
I also read the Stack Overflow question “start %comspec% /c script.cmd” vs “start cmd /C second.cmd script.cmd”, which made me replace the "%comspec%" statement with "C:\Windows\system32\cmd.exe", but that did not change anything either.
The problem is that now, instead of a minimized running bat file, I end up with just a command prompt, minimized but without any of the batch commands executed. The task scheduler status remains "running" :(
How do I get this done on Windows 8 (64-bit)? Preferrable with old-school batch commands instead of PowerShell (or worse ;p)
The start command needs the leading "" quotes to disable the title feature. Try scheduling this:
%comspec% /c start "" /min "C:\Scripts\Destination_inbound_ftp5.bat" ^& exit
Assuming Windows 8 is the same as Windows 7, an "exit" is only going to exit the batch file (which it is going to do anyway).
You need to add the exit code like this:
Under "Program/Script":
CMD (or command.exe, or %comspec%)
Under "Arguments:
/c start "Title" /min "C:\Scripts\Destination_inbound_ftp5.bat" ^& exit
I didn't like seeing the command window pop up and then disappear so here is another solution from https://ss64.com/vb/run.html ...
First create invisible.vbs with this single line of text:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Next and finally, launch your cmd or batch file via:
%SystemRoot%\system32\wscript.exe "invisible.vbs" "myscript.cmd" //nologo
Ta da! Scripting of this sort has been built into Windows for a long time. If you're curious, do a web search for "WSH" (windows scripting host). You can even write such scripts in dialect of JavaScript called JScript.
Another possibility: a small freeware program named CMDH, that simply runs the requested orders in background.
For example:
cmdh MyScript.cmd
No need to add "exit" to the script.
Tested working in Windows XP SP3, and there is no reason it should fail on Windows 8.
Here's a solution from https://ss64.com/vb/run.html that will run a batch file in a minimized window. Unlike the other solutions that use the start command with /min, this one will not flash a new window onto your screen or interrupt full-screen activities. It does, however, steal focus. I don't know how to avoid that.
First create a file named run_minimized.vbs with this single line of text:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 2, False
Next, create your Task Scheduler task with an action to start the program wscript.exe with these arguments:
"c:\path\run_minimized.vbs" "c:\path\my script.bat"
Change the paths as necessary to specify the locations of the two files.
There is no simple way to pass arguments from Task Scheduler to the batch file while also preserving spaces and quotation marks, because wscript strips quotation marks from its arguments. The simplest way to handle arguments with spaces would be to put the entire batch file command into the vbs:
CreateObject("Wscript.Shell").Run """c:\path\my script.bat"" ""arg 1"" arg2", 2, False
Note the use of quotation marks. There's one pair of quotation marks " enclosing the entire command string, and a pair of adjacent quote characters "" every place you'd use a normal quotation mark in a command line.
Maybe it's not the same as you mean but if I simply run a .bat file with the scheduler and tick this "Hidden" box on the General tab of the task properties it starts minified.
As already mentioned in foxidrive's answer, the issue is caused by the missing title parameter, which is taken from the first quoted argument string on the command line of the start command, which is the quoted batch file path in your command line. Since there is nothing left to be taken as the command/program to be started, the default cmd.exe is started.
Providing a dummy title (which may even be empty) solves that issue:
%ComSpec% /C start "" /MIN "C:\Scripts\Destination_inbound_ftp5.bat"
However, as mentioned in the help message of start (type start /? into a command prompt window), when the provided command/program is a batch file, the command processor cmd.exe is run with the /K switch, which keeps the command prompt window open:
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
To prevent that, let us explicitly specify the command processor with the /C switch:
%ComSpec% /C start "" /MIN %ComSpec% /C "C:\Scripts\Destination_inbound_ftp5.bat"
So there is no additional exit command necessary.

Resources