2

In Windows if I have the PID for an application and I want to either focus or close the app via the command prompt.

What commands do I need to run?

e.g.

FOCUS 123

or

CLOSE 123

Where 123 is the PID.

3 Answers 3

2

To kill an app instance using its PID, use taskkill /pid <pid> (tasklist will list all the running processes).

To switch focus to an app instance using its PID, use the VBScript below:

set objShell = WScript.CreateObject("WScript.Shell")
objShell.AppActivate WScript.Arguments.Item(0)
objShell.SendKeys "% x"

The MSDN documentation says that % represents ALT. Together with SPACE we can access the Alt-Space popup menu, and use an keyboard accelerator to maximize or restore the window.

If you save it as Focus.vbs, you can invoke it from the command line using:

wscript Focus.vbs <pid>
6
  • Actually there is a nasty bug with this. If the window is minimized then it makes it impossible to focus any other window as it tries to focus the window but can't and just flickers. Is it possible to get it to focus windows that have been minimized?
    – Cameron
    Jun 22, 2015 at 13:35
  • Minimized windows can actually receive focus while remaining minimized. If you want the minimized window to be un-minimized/restored along with receiving focus, let me know and I'll see what can be done about it.
    – Karan
    Jun 22, 2015 at 13:38
  • Yes, I'd like it to un-minimized/restored and then focused so that this bug can't happen. Thanks.
    – Cameron
    Jun 22, 2015 at 13:39
  • Ok, give me some time please. BTW, on my system the minimized window's taskbar button lights up showing it has received focus, but there's no flicker and I can easily click on and switch focus to other windows. Not sure what's causing the problem on your system.
    – Karan
    Jun 22, 2015 at 13:40
  • I think it;s the loop that causes it, because it hasn't managed to focus the application properly and thereforce causes it to keep trying and therefore prevents other windows from being interacted with. I've disabled the loop, so it's just a case of doing a restore and a focus and should be fine.
    – Cameron
    Jun 22, 2015 at 13:50
3

From windows command taskkill:

taskkill [/s Computer] [/u Domain\User [/p Password]]] [/fi FilterName] [/pid ProcessID]|[/im ImageName] [/f][/t]

Example: taskkill /pid 123

Source Microsoft

3

How do I kill a process using it's PID?

You can a process using tskill:

tskill 123

Syntax

TSKILL processid | processname [ /SERVER:servername] [ /ID:sessionid | /A] [ /V]

Source tskill


How do I set focus to a process using it's PID?

To set the focus to an application you will need to use a 3rd party application, such as Nircmd Win.

nircmd win focus process /123

Syntax

nircmd win [action] [find] [window to find] [Additional Parameters]

Source Nircmd Win

4
  • I get the error that win is not recognised as an internal or external command.
    – Cameron
    Jun 16, 2015 at 13:12
  • Can I focus an app without an external program? Thanks
    – Cameron
    Jun 16, 2015 at 13:16
  • You may be able to do something with powershell.
    – DavidPostill
    Jun 16, 2015 at 13:41
  • win is actually nircmd's subcommand, that is full syntax is nircmd win focus process /123.
    – user373230
    Jun 29, 2015 at 0:48

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.