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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityTo 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>
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
You can a process using tskill
:
tskill 123
Syntax
TSKILL processid | processname [ /SERVER:servername] [ /ID:sessionid | /A] [ /V]
Source tskill
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
win
is not recognised as an internal or external command.
win
is actually nircmd
's subcommand, that is full syntax is nircmd win focus process /123
.