This tutorial explains how to launch a program and exit it using a batch script.

Developers often write scripts to automate tasks such as opening an application, performing operations on it, and then closing it.

@echo off
start "title" program
exit

@echo off: This command turns off command execution echoing to the console. start: This is a DOS command used to open a new window with the specified file, terminal, or application. title: This is the title of the new window, which can be left empty. program: This is the name of the application or file to be opened. exit: This command closes the window. You can also use /s to exit the new window.

For example, to start the Calculator application from a batch script:

The below code opens the new window, starts the calculator and new window is closed, using the exit command

@echo off
start "title" calc
exit

How to Open and Close a Program from a Batch Script

In this example, we’ll open Notepad and then close it using the exit command.

Let’s create a openclose.bat file:

@echo off

rem Open Notepad
start "" "Notepad.exe"

rem Timeout for 3o seconds before closing
timeout /t 30 /nobreak >nul

rem Close the Notepad
taskkill /f /im "Notepad.exe"
exit

In this example:

The start command is used to open the Notepad application, starting the Notepad process and opening the application. We then wait for 30 seconds using timeout with the /t option, allowing no keypress before the timeout. Finally, the taskkill command is used to terminate the process.