I have 4-5 process (like java.exe, javaw.exe etc) having username "OWNER"(suppose). Below is the script that filters the java.exe process and kills it if it belongs to "OWNER". I need your help to modify this so that any process related to "OWNER" would be killed if found.
2 Answers
Just do it with Get-Process:
get-process -IncludeUserName | where username -like $username | stop-process
Basically your whole script can be replaced with this line
1 Comment
RonyA
V3 don't have includeusername
Get-Process with -IncludeUsername switch is only available in WMF 5.0.
WMI is the option here.
You could probably terminate the process just by checking the owner equals to the corresponding user.
Get-WmiObject -Class Win32_Process | Where-Object -FilterScript {
$_.GetOwner.User -eq "$Owner" } | Invoke-WmiMethod -Name Terminate
Edit: The above code is a one liner, you could save the out put of Get-WmiObject in a variable and for foreach through the collection to print the process id and call the terminate() method instead of using Invoke-WmiMethod.
Note:This code is not tested
3 Comments
RonyA
This seems to be good idea . Although I first want to print the process ID and want to kill that.. How to do that with the command you typed ..
Prasoon Karunan V
You will not get a ready made script for your task from StackOverflow, I have edited the Answer by giving you max hint.
RonyA
In the actual script I pasted I want that to be used as some dependency on that section . Hope you got the point . I will try to club all the help you guys provided .
@{}gwmi win32_process- this doesn't look right