-4

Using Win2019 SRV, I have a process (process_name) in the tasklist.
Each user starts a new process_name (the same process) and in task manager I could see the same process with different user into "user column".
Users are User1, User2, User3, ...

So, I would like to kill the process_name started by User2, leaving the other, using a script.
With this batch file code I can find the process name and kill it

Tasklist | Findstr /I “process_name”
Taskkill /IM “process_name”

but in this way I'm killing this process for all users.
"Taskkill /U" select user that execute the command.

How can I select only processes executed by User2?

Thank you

Squashman
  • 13,649
  • 5
  • 27
  • 36
Valentino
  • 33
  • 1
  • 6
  • 2
    `TASKLIST` and `TASKKILL` both have options to specify the user name. Open up a command prompt and type: `tasklist /?` and `taskkill /?` to read the syntax usage. – Squashman Oct 18 '21 at 13:44
  • 1
    As above, reading the help and usage information should show you the syntax you need. Therefore in a [[tag:batch-file]], try `@%SystemRoot%\System32\taskkill.exe /Fi "UserName Eq User2" /Im Process_Name`. Please in future, do not ask questions without having first read the built-in help information for the command you require assistance with. _This is not your first interaction on this site, and you should know better._ Please also note that your submitted code is using incorrect smart, _(curly)_, double-quotes, and not the correct, dumb, _(straight)_, double-quotes. – Compo Oct 18 '21 at 13:53

2 Answers2

0

In PowerShell this should work (must run as admin)

Get-Process -Name 'process_name' -IncludeUserName | 
Where-Object { $_.UserName -eq "User2" } | Stop-Process -Force

Beware that the username is in the form of domain\username

Theo
  • 57,719
  • 8
  • 24
  • 41
0
Get-Process program.exe -IncludeUserName | Where {$_.UserName -match "abc" | Stop-Process

Try this from an elevated command prompt - PS v5

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Dilly B
  • 1,280
  • 2
  • 11
  • 15