Windows – Close programs from the command line (Windows)

command linewindows

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

TSKILL chrome:

(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?

Best Answer

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 /IM process.exe

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

taskkill /IM chrome.exe >nul

Summary: TASKKILL is the proper way via command line to close applications per its WM_CLOSE implementation and the Microsoft KB that I linked.