Is there a way to close a program that's currently in focus from the command line?

I’m running a voice control program that executes commands in the background. The only way I know to do it is with the taskkill command, which requires me to put in a specific process name. I want it to kill whatever process is currently in focus, so if Firefox is focused, it kills that and if notepad is focused, it kills that.

I'm using win 10 in case that's relevant.

  • You can't do that using built-in Windows utilities, but you can do that with programs such as AutoHotKey. Mar 18 '20 at 20:40

Unfortunately, there’s no linkage between the command environment and the GUI. When you use taskkill to kill an application, it’s acting on the process, not the window. This is an important distinction because by killing a process, you’re doing it forcefully; the same as if it has crashed. This probably isn’t what you want to be doing most of the time.

Normally, pressing Alt+F4 will close the currently focused window (or Esc if it’s a dialog box). This shortcut is equivalent to choosing File -> Quit from the menu bar or clicking the Close button on the title bar.

To do it from a command prompt, you’d need a script that can identify the active window and send it keystrokes. This can be done with PowerShell. The Windows Script Host (i.e. VBScript or JavaScript) can do it too. But it would require some programming skills. Programs like AutoHotKey or KiXtart are another option.

Basically, you’d be calling the script from the command line via your voice control program.

Note that the application will do its normal closing routine (e.g. prompting you to save your work, etc), so it’s not a forced termination the way taskkill would do it.

  • Yes, that would work. However, I'm using a program to execute windows shell commands in the background and I don't think you can use ALT+F4 in windows shell, can you?
    – Aci
    Mar 19 '20 at 8:25
  • @Acituanbus; When you say ‘Windows shell’, what are you referring to? The command prompt? And what program are you using to execute commands in the background?
    – Wes Sayeed
    Mar 19 '20 at 8:30
  • Yes, the command promp. It's called LINKS Mark 2, it's a just-for-fun voice-controlled program
    – Aci
    Mar 19 '20 at 8:34
  • @Acituanbus; I edited your question and updated my answer to reflect that information. Basically, you’ll need some scripting skills. There’s no silver bullet for what you want to do.
    – Wes Sayeed
    Mar 19 '20 at 9:17

Is there a way to close any program that's currently in focus via the Windows Shell?

That's not possible because in order to type any command in the windows shell you must switch focus to the shell.

The previously focused program (window) will lose focus.

As far as I know there is no windows shell command that will close the previously focused program.

  • I'm using a program that executes shell commands in the background and want it to kill the current window in focus, so it should be possible
    – Aci
    Mar 19 '20 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.