![How to close command prompt after batch job completes How to close command prompt after batch job completes](/uploads/1/2/3/9/123925477/733660808.png)
By default, new versions of Windows will automatically close the Window of any program, including an MS-DOS program or batch file, when the program terminates. Unfortunately, if that MS-DOS command or program outputs or displays any information that the user needs to read, they will be unable to read it because it closes too fast.
Nov 19, 2011 - Give me.one. command, people. Spelled it right ('sudo' as opposed to 'pseudo') and gave me a list of sandwich restaurants close to me.
This issue can be resolved by doing any of the below recommendations.
NoteUnfortunately, there is no setting in Windows for disabling the auto-closure of the MS-DOS windows.
Open an MS-DOS window
If you are running an MS-DOS command or program from within Windows, try opening an MS-DOS prompt and executing the command or program from within MS-DOS.
Change the path of the command in the shortcut
Microsoft Windows 2000 and Windows XP users can execute the command through CMD.EXE using the /K option. Doing so will open a command window and then executes the command or program. After the command or program has completed, the computer will remain at the command prompt. To do this, follow the steps below.
- Create a shortcut to your MS-DOS program or command.
- Once a shortcut is created, right-click the shortcut and click Properties.
- In the Properties window, change the Target location to contain the CMD.EXE command with the /K switch as shown in the example below.
Before
After
- See the CMD command page for further information about this command.
Add a pause statement to a batch file
![Staz Staz](http://www.postgresqltutorial.com/wp-content/uploads/2015/07/psql-command-ef-edit-function.jpg)
Finally, it is important to realize that the window will not close if Microsoft Windows believes the program is still running. If you are creating a batch file and want the MS-DOS window to remain open, one trick is to add PAUSE to the end of your batch file, which will prompt the user to press any key. Until the user presses any key, the window will remain open instead of closing automatically.
Additional information
- See our close definition for further information and related links.
For decades, you've been able to hold down [Shift] and Right-Click on a folder (or the background of a folder) to get a 'Open Command Prompt here' option. ('Copy as path' is also on this menu)
I recently also discovered, that you can open any program using the folder as the current folder by typing a command (e.g. cmd.exe or powershell.exe) in the Address Bar.
In the Windows 10 Creators Update, the default shell is being changed to PowerShell ('Open PowerShell here') -- in light that PowerShell is a more comprehensive shell than CMD.
Looking into how this works, I discovered and realized two things:
- Sometimes I want more than just a single shell option in the menu
- Command Prompt
- PowerShell
- GIT
- Visual Studio Developer Tools
- etc.
- With the introduction of UAC in Windows 7, it's no longer possible to launch one of these shells/applications elevated
The first issue isn't an issue. Adding additional entries is easy -- it just requires additional 'shell<verb>command' registry entries.
- The order in the menu is the alpha order of the <verb>
- The menu text is the 'shell<verb>(default)' REG_SZ text
- i.e. the menu doesn't have to be in the alpha order of the menu text
- Add '&' in the text to specify the keyboard shortcut
The second observation - the elevation - is the issue. If the target EXE isn't manifested as requiring elevation, you need to use the special verb 'runas' to cause the elevation. That's great if you're just adding one entry, but problematic if you want more than one.
What you have to do is to use the 'ExtendedSubCommandsKey' concept. This creates a sub-menu. Each sub-menu can have a 'runas' key, allowing multiple elevated entries in a context menu tree (one per menu/sub-menu).
The great thing about the 'ExtendedSubCommandsKey' concept is that the value is a 'reference'. Meaning a single configuration can be shared between the folder's node ('Directoryshell...) and the folder's background ('Directorybackgroundshell...) context menu registrations. Both areas point (reference) the 'DirectoryContextMenusshell...' area -- saving you the need to duplicate the <verb> settings.
Within each sub-menu (each ContextMenu area), you make the ...shell<verb>... keys as normal. The only exception is the menu's name; it is set via a 'MUIVerb' REG_SZ, rather than the usual '(Default)' REG_SZ.
In my example below, I have the Command Prompt and PowerShell supported as sub-menus. Within each sub-menu, there is a verb to launch the prompt non-elevated ('open') and elevated ('runas').
- Command Prompt support
- HKEY_CLASSES_ROOTDirectoryshell01MenuCmd --> DirectoryContextMenusMenuCmd
- HKEY_CLASSES_ROOTDirectorybackgroundshell01MenuCmd --> DirectoryContextMenusMenuCmd
- HKEY_CLASSES_ROOTDirectoryContextMenusMenuCmd
- open --> Command Prompt
- runas --> Command Prompt Elevated
- PowerShell support
- HKEY_CLASSES_ROOTDirectoryshell02MenuPowerShell --> DirectoryContextMenusMenuPowerShell
- HKEY_CLASSES_ROOTDirectorybackgroundshell02MenuPowerShell --> DirectoryContextMenusMenuPowerShell
- HKEY_CLASSES_ROOTDirectoryContextMenusMenuPowerShell
- open --> PowerShell
- runas --> PowerShell Elevated
What you get is something like this (this example is a right-click of a folder's node):
PowerShell menu
You don't have to use it run an application elevated to not. For example, with GIT, it can be use to launch the GIT GUI or GIT BASH.
- GIT Prompts
- HKEY_CLASSES_ROOTDirectoryshell03MenuGIT --> DirectoryContextMenusMenuGIT
- HKEY_CLASSES_ROOTDirectorybackgroundshell03MenuGIT --> DirectoryContextMenusMenuGIT
- HKEY_CLASSES_ROOTDirectoryContextMenusMenuGIT
- git_gui --> GIT GUI
- git_shell --> GIT BASH
Icons, UAC and hiding other menus
To make make things look pretty/orderly, I've set the following to add the appropriate icon, indicate UAC, and to hide (unwanted) menus.
- Set the application's icon via the 'Icon' REG_SZ on the parent and child nodes
- Enable the UAC icon via the 'HasLUAShield' REG_SZ on the elevated nodes
- Note, this has no effect on whether elevation is applied or not. It's purely cosmetic.
- If Icon is also set, it's a tiny icon in the bottom right
- If Icon isn't set, it's a full sized icon of the shield
- Add the 'Extended' REG_SZ on the (old) settings that you want to hide in the Shift Right-Click menu
- I add these, instead of deleting the entire node because A) TrustedInstaller only has access B) Installers will put them back again
- You'll get a error/warning when merging the registry file if they are owned by TrustedInstaller (not an issue)
ENJOY!
Example: Directory_Prompts.reg
Example: Directory_Prompts_GIT.reg