Search code examples
pythonwindowssubprocesswmic

WMIC + Python Subprocess/ (Windows 10)


I'm trying to kill an .exe with subprocess.Popen.

Solution as:

  1. taskkill /im (.exe) /t /f
  2. taskkill /pid (.exe) /t /f

aren't working as the response is access denied. Indeed, as i'm running the cmd from subprocess i'm not able to obtain admin privilegies.

I've found a command to kill this process from cmd (without running it as admin) which is:

  1. wmic process where name=".exe" delete

... but when i'm running it with subprocess it gives me "invalid query". The command i'm running is:

4)subprocess.Popen(['wmic', 'process', 'where', 'name="-------.exe"', 'delete'], shell=True, stdout=subprocess.PIPE)

I suppose that i'm writing it wrongly. Some advices?


Solution

  • The solution is to put spaces around the comparison operator and send the whole condition (name = xxx or name like xxx) as single argument, since wmic doesn't like it to be split in parts, and internally the quotes get messed up if they are in the "middle" of a word:

    subprocess.Popen(['wmic', 'process', 'where', 'name like "----.exe"', 'delete'], shell=True)