I've inherited an environment in which two different processes use identical names but their executable files are stored in different file paths. None, one, the other, or both may be running at any time.

When I perform certain operations (like patching), I want the process associated with the patching directory to be terminated but not the other one.

Using a variety of different techniques, I have no trouble terminating the processes based upon process name, such as (less elegantly without using powershell):

echo Checking for procname process...
tasklist /FI "IMAGENAME eq procname .exe" | !windir!\system32\find /I "procname.exe" 
if !EL! EQU 0 (
    echo Killing procname process...
    TASKKILL /F /FI "IMAGENAME eq procname.exe"

or more elegantly (using powershell)

powershell Clear-Host 
powershell Stop-Process -name procname

However, both of these methods kill both processes if they're running.

Instead of killing these processes based upon their process name, how can I kill a process based upon the path to their executable?

  • Sorry, I've no time right now to flesh this out right now, but here's some ideas: WMIC PROCESS WHERE name="calc.exe" DELETE and WMIC PROCESS LIST FULL – TOOGAM Mar 14 '16 at 19:10

I found a way to do what I want and thought I'd share the solution.

This solution requires powershell.

powershell Get-Process <ProcessName> ^| Where-Object {$_.Path -like '*<path_fragment>*'} ^| Stop-Process

** the "^|" pipes are required to keep the pipes within the powershell interpreter. Using a normal pipe ("|") seems to pipe it back to the normal command interpreter and breaks the command.

To put some real values behind this code, assume the process to kill is Notepad++:

powershell Get-Process notepad++ ^| Where-Object {$_.Path -like '*Notepad++*'} ^| Stop-Process

This can be invoked on a command line from within a batch file (which is my intention).

If users might see the output from the command above, put a stderr redirect to nul at the end of the line. When the Get-Process doesn't find something, it issues what appears to be a fairly severe error -but is really just its way of saying "I didn't find anything."

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.