- Windows cmd close window
- How to close cmd or command window after executing a program
- Get the CMD.exe Window to close
- [Force default gadgets to load on the desktop when a user logs in]
- Windows cmd close window
- Close programs from the command line (Windows)
- 5 Answers 5
- Update to Updated Question
- How to automatically close cmd window after batch file execution?
- 10 Answers 10
- Not the answer you’re looking for? Browse other questions tagged batch-file cmd or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
Windows cmd close window
How to close cmd or command window after executing a program
Get the CMD.exe Window to close
Do you ever have a bat or cmd file that you have written to do something like update a config file then launch a application or process, and have it fail to close the cmd window after it completes even with the “EXIT” command placed at the end of script?
Yes? Funny… me too.
Here is what I found fixes the issue.
I have a script that updates the desktop gadgets your profile should launch upon login. Pretty simple script, it copies the settings.ini file from a network share and places it in your profile directory under “sidebar” and then launches sidebar.exe with a switch. This is a great script for controlling the gadgets seen on a users desktop. (Script example below if you want it)
The problem I was having with this script was I was trying to launch the sidebar application after the update and that was causing the cmd window not to close after it launched the application.
C:\Program Files\Windows Sidebar\sidebar.exe” /autostart
Here is the trick to get a cmd that fails to release after launch in a batch file to actually release.
start “” “C:\Program Files\Windows Sidebar\sidebar.exe” /autostart
If you look at the command, I use the “start” cmd and then supply (“”) a literal nothing as a switch to the “start” cmd. Then in quotes (if spaces exist) the command to run.
Wow, the window now closes after it launches the sidebar application.
I hope this helps some scripter out there with login scripts they are writing. Below you will find a copy of my login script to set gadgets up on Desktop when a user logs in.
Here is a little more about the “load default gadgets at login” script.
[Force default gadgets to load on the desktop when a user logs in]
How to force default gadgets on a users desktop, First off to get started you need to setup a profile and launch the gadgets you want on the desktop. Move them where you want them to be on the desktop so that they are placed in the areas you want them. We have dual monitors across our tech team so we placed all gadgets on second monitor.
Now go grab the config file you just created by moving and launching the gadgets you wanted. The default location for the gadget config file in a users profile is:
Lastly we create a batch file to recopy this file each time a user logs in and then launches the sidebar application.
Create a new batch file called gadgets.bat then place the following in the file.
REM @echo off
taskkill /IM sidebar.exe /f
copy /Z /Y \\myserver\myshare\Settings.ini “%userprofile%\AppData\Local\Microsoft\Windows Sidebar\”
start “” “C:\Program Files\Windows Sidebar\sidebar.exe” /autostart
Copy the Settings.ini file to a network share. Edit the “myserver” and the “myshare” to represent your network share that holds the “Settings.ini” file.
Then add it to your users login script active directory properties. You can also just copy and paste the lines in to an existing login script if your using them to do other things like mapping drives or printers and such.
Windows cmd close window
what about this code:
regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
Since I would assume the Skype executable is at least a Win32 program, I find it peculiar that the window does not close. How are you launching the batch file? From a command line or from an explorer window? If the later the window should close. If the former, the batch procedure needs to execute an EXIT statement.
Anyway, try this .
@echo off
start «» «C:\Program Files\Skype\Phone\Skype.exe» /secondary /minimized
exit
This should make doubly sure that the command window closes.
Since I would assume the Skype executable is at least a Win32 program, I find it peculiar that the window does not close. How are you launching the batch file? From a command line or from an explorer window? If the later the window should close. If the former, the batch procedure needs to execute an EXIT statement.
Anyway, try this .
@echo off
start «» «C:\Program Files\Skype\Phone\Skype.exe» /secondary /minimized
exit
This should make doubly sure that the command window closes.
What? — You don’t trust Windows.
regards Thomas Paetzold visit my blog on: http://sus42.wordpress.com
Sorry peddy but you know that that won’t work since you wrote the book on the Win32 API.
It works for my case.
Thanks everyone very much for your suggestions
@echo off
start «» «C:\Program Files\Skype\Phone\Skype.exe» /secondary /minimized
Thanks in advance for any suggestions
It’s my opinion that exit without the /b parameter should not be used to end a shell script (batch file). I find it rather annoying when I run a script at the command line and it closes my command window for me. (That’s just me of course)
I see your point, yet in this particular case the stated object was to force the command window to close. It turns out the exit was not required and was therefore not appropriate. I threw it in just to be sure, as I said. I had failed to realize the fact that Win32 executable now run synchronously in command windows. In earlier Win versions, especially the Win9x varieties, they always ran asynchronously, unless you forced them to wait wth the Start utility. I don’t know what OS introduced that twist. Either that or the Skype executable is actually a true command console application.
It looks very wired and I am not sure is all that necessary, and
— Why some simple task must be so complicated, in fact it looks little nonsense , with this we are cheating this script and same Windows .
However It works .
But can it be little easier for one simple Bach file .
Close programs from the command line (Windows)
What is the proper way to close/exit programs from command line, similar to pressing the «X» close button in the corner of the window?
Im trying to close chrome under 3 different versions of windows: win7 ultimate, win7 home, winXP
Under Ultimate and XP: TSKILL chrome
Under Home: TASKKILL /IM chrome.exe
(It closes chrome, no cmd errors,
but chrome error restore when relaunch it)
TASKKILL /IM chrome.exe :
(It closes chrome, no chrome errors when relaunch it,
but errors in cmd: «impossible to terminate child processes(about 4-5), only by force with /F «)
Should I ignore cmd child errors if on relaunch chrome show me no errors?
5 Answers 5
The proper way to close/exit a program ultimately depends upon the software. However, generally the best practice is for Windows programs to close whenever they receive the WM_CLOSE message. Properly releasing memory and closing handles. There are other messages that can signal the close of the application, but it is up to the author of the software how each message is handled.
taskkill sends the WM_CLOSE message and it is then up to the application whether to properly close. You may also want to use the /T option to also signal child processes.
Only use the /F option if you want to force the termination of the process.
Other options would include sending the Alt+F4 keys, using PowerShell, or 3rd party applications.
Update to Updated Question
Ignore, the errors. Chrome generates many processes. The errors are caused when an process does not acknowledge the WM_CLOSE message that TASKKILL sends. Only processes with a message loop will be able to receive the message, therefore, the processes that do not have a message loop will generate that error. Most likely, these processes are the chrome extensions and plugins.
To hide the errors capture the output
Summary: TASKKILL is the proper way via command line to close applications per its WM_CLOSE implementation and the Microsoft KB that I linked.
It has «closeprocess» command which is designed to close processes gracefully. As per its document, it does not terminate apps but sends WM_CLOSE to all top-level windows of the target process.
If this doesn’t work, I bet your application has an unusual cleanup procedure. I would like to see what happens inside so please let me know what your target application is.
The answer to that question can be found here (Microsoft link).
You can send WM_CLOSE messages to any window you wish to close. Many windows handle WM_CLOSE to prompt the user to save documents.
A tool that does this correctly is @Kill . Look also SendMsg.
I do not know how to do this in batch, but you could use the vbscript for this. Simulating the Alt + F4 keys (equates to signal WM_CLOSE ).
Run and look at the behavior of this script below.
Here is the list key names for SendKeys.
When you run the script, the notepad is open, some words are written and then a signal to close the program is delivered, see picture below.
Additional Questions
Can I start a program minimized, or background with vbscript?
Yes. Use the following code:
For more information check the Run Method .
Can chrome go to some url in vbscript?
If chrome is the default, use:
Is there a way to bring focus to specific application (chrome.exe)?
I want to send alt+f4 ONLY to chrome, independently of i’m doing with other windows.
The following code works on Windows 8.
There are some command-line utilities that can send a suitable WM_SYSCOMMAND message (with SC_CLOSE as the command) to a program’s top-level window. I’m sure that at least one will be mentioned shortly. (Then someone will mention AutoIt. Then there’ll be an answer showing how to do it with PowerShell and CloseMainWindow() .)
The command-line utility that comes as a built-in command in JP Software’s TCC, a command interpreter and command script processor for Windows, is called TASKEND .
Alright, Not going to lie. I saw this on stackoverflow and thought it was a challenging question. Soooo I just spent the last 2 hours writing some code. And here it is.
After following the steps below, you can type «TaskClose notepad.exe» after hitting «Start» and it will auto save all undocumented notepad files into desktop. It will auto-close chrome.exe and save the restoration settings.
You can add and remove additional settings for other applications under the if conditions. For instance:
The vbs and batch files performs the following procedures:
- Collects the executable.
- Queries the executable application names off of the tasklist.
- Performs an «Alt+TAB(x)» procedure until it has verified the window is open.
- Then Executes the rolling commands whether it be «Alt+F4» or even in extreme cases
- Alt+F4
- Activate Save
- AutoIncrememnt Filename
- Exit application.
ReturnAppList.bat : install in «C:\windows\system32\»
TaskClose.bat : install in «C:\windows\system32\» AND «C:\Users\YourUserName\»
TaskClose.vbs : install in «C:\windows\system32\»
This was alot of fun to write and I’m more happy about finishing it than actually showing the answer. Have a great week!
How to automatically close cmd window after batch file execution?
I’m running a batch file that has these two lines:
This batch file is used to run the Xming application and then the PuTTY app so I can SSH into my university’s computer lab.
However, if I run this and Xming is not already open, once I exit from the PuTTY terminal the cmd window remains open. Only if I have already run Xming does the cmd window close when I close the PuTTY terminal. I’ve tried adding exit to the last line of the batch file, but to no avail.
10 Answers 10
Modify the batch file to START both programs, instead of START ing one and CALL ing another
If you run it like this, no CMD window will stay open after starting the program.
You normally end a batch file with a line that just says exit . If you want to make sure the file has run and the DOS window closes after 2 seconds, you can add the lines:
But the exit command will not work if your batch file opens another window, because while ever the second window is open the old DOS window will also be displayed.
SOLUTION: For example there’s a great little free program called BgInfo which will display all the info about your computer. Assuming it’s in a directory called C:\BgInfo , to run it from a batch file with the /popup switch and to close the DOS window while it still runs, use:
If you want to separate the commands into one command per file, you can do
and in the other file, you can do
The command cmd /c will close the command-prompt window after the exe was run.
This worked for me. I just wanted to close the command window automatically after exiting the game. I just double click on the .bat file on my desktop. No shortcuts.
To close the current cmd windows immediately, just add as the last command/line:
Try move nul to nowhere and redirect the stderr to stdin will result in the current window cmd.exe being closed
This is different from closing a bat, or exiting it using goto :EOF or Exit /b
I even opened a question to find an answer that would explain behavior. Would that be a bug? Immediate closing of the current cmd window when executing: move nul 2>&0
I had this, I added EXIT and initially it didn’t work, I guess per requiring the called program exiting advice mentioned in another response here, however it now works without further ado — not sure what’s caused this, but the point to note is that I’m calling a data file .html rather than the program that handles it browser.exe , I did not edit anything else but suffice it to say it’s much neater just using a bat file to access the main access pages of those web documents and only having title.bat , contents.bat , index.bat in the root folder with the rest of the content in a subfolder.
i.e.: contents.bat reads
It also looks better if I change the bat file icons for just those items to suit the context they are in too, but that’s another matter, hiding the bat files in the subfolder and creating custom icon shortcuts to them in the root folder with the images called for the customisation also hidden.
You could try the somewhat dangerous: taskkill /IM cmd.exe ..dangerous bcz that will kill the cmd that is open and any cmd’s opened before it.
Or add a verification to confirm that you had the right cmd.exe and then kill it via PID, such as this:
Substitute (pslist &&A) or (tasklist /FI «PID eq %%A») for the (taskkill /PID %%A) if you want to check it first (and maybe have pstools installed).
Just try /s as listed below.
As the last line in the batch file type:
The above command will close the Windows CMD window.
/s — stands for silent as in (it would wait for an input from the keyboard).
This works for me
Sometimes you can reference a Windows «shortcut» file to launch an application instead of using a «.bat» file, and it won’t have the residual prompt problem. But it’s not as flexible as bat files.
Not the answer you’re looking for? Browse other questions tagged batch-file cmd or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.