I need a batch file that opens cmd, runs two commands, and keeps cmd open

11,688

I need a batch file that opens cmd, runs two commands, and keeps cmd open

taskkill /F example.exe

This is an invalid command. You need to use:

taskkill /F /IM example.exe
  • /IM image name. The image name of the process to be terminated. Wildcard '*' can be used to specify all image names.

Use the following batch file (test.cmd):

@echo off
setlocal
taskkill /F /IM example.exe
start example.exe
endlocal

To run it and keep the cmd shell open use the following command:

cmd /k test
  • /K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • cmd - Start a new CMD shell and (optionally) run a command/executable program.
  • start - Start a program, command or batch script (opens in a new window).
  • taskkill - End one or more processes (by process id or image name).
Share:
11,688

Related videos on Youtube

jakob qasadilla
Author by

jakob qasadilla

Updated on September 18, 2022

Comments

  • jakob qasadilla
    jakob qasadilla over 1 year

    I am making a set of batch files made to force restart games when they freeze and won't start back up again.

    So I need a batch file that opens cmd.exe, runs the two commands:

    taskkill /F example.exe` 
    

    and

    start example.exe
    

    and lets cmd stay open. Every time I try these two commands cmd is just open for a second and closes saying that no command was made, or the commands aren't able to be used even though they work when I type them into cmd directly.

    • jakob qasadilla
      jakob qasadilla almost 8 years
      I found out that I was encoding my batch files as unicode instead of ANSI but I wouldn't have known how to do these commands anyways if I didn't post this question