How to close cmd or command window after executing a program

On May 17, 2011, in How-to, Scripting, by Cubert aka (Cube Dweller)

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:

%userprofile%\AppData\Local\Microsoft\Windows Sidebar\Settings.ini

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.

Download script -> Launch Default Gadgets batch file

 

Tagged with:
 

13 Responses to “How to close cmd or command window after executing a program”

  1. daigoumee says:

    Terrific work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher!

  2. Leigh says:

    Thank you very much!!!!

  3. Sarah says:

    Thank you so much! I have been searching and searching for this!!

  4. Joe says:

    So many failed solutions. This works actually works. Thanks!

  5. Alasdair GF says:

    I’m struggling with this… I have a really simple batch file that starts another batch file (a workaround as I can’t figure out a relative path shortcut for a file on a removable drive):

    @echo off
    rem Starting default batch file – no messing around!
    start “Starting IDLE” “App\Lib\idlelib\idle.bat”
    exit

    Unfortunately, this doesn’t close the cmd.exe window at all. Any pointers here?

    (Also, what does “/autostart’ do? My google-fu has failed me, results seem unrelated…)

  6. Shannon Anderson says:

    Actually the Autostart is a typeO to autorun, a switch sent to sidebar.exe.

    Have you tried,

    @echo off
    rem Starting default batch file – no messing around!
    start “” App\Lib\idlelib\idle.bat

    where “” is a empty set of quotes first before the real command? then I do not see a full path to script so it may have a hard time finding where idle.bat really is?

    Cubert

  7. John says:

    you can also throw in the “/c” switch after a “start cmd” command to close the CMD window when its finished.
    I use it at the same time I set colors to the CMD windows, see example:

    Ex:

    start cmd /T:F1 /c robocopy “source” “destination” /mir

    /k instead of /c keeps the windows “alive” 🙂

    //John

  8. John says:

    Thank you! One of the most useful tips! Totally made my day! 🙂

  9. Rick says:

    Great Tip!!
    Thanks a lot.

  10. Pseudoname says:

    Thanks !!!!

  11. Lucia says:

    It works!! Thank you so much!!

  12. Greg says:

    Nice! Just made my life a little better!

  13. BoB says:

    Thanx! It works!

Leave a Reply