Author Posts

August 25, 2016 at 12:30 pm

Hi there,

I'm just learning the basics of powershell and I wanted to resolve the following issue:

I need to kill a specific task that is run on a remote computer by all users that are not currently logged in.

I want to be able to run the command on a Windows 7 machine.

What I got so far:

Get-WmiObject win32_process -computername AAA | select name,@{n="owner";e={$_.getowner().user}} | where{$_.name -like "*lync*"} | where{$_.owner -ne "BBB"}

this gives me all processes with *lync* on the computer AAA that are not from User BBB. So far so good. but how would I end these processes now?
I tried with foreach {$_.terminate()} but that did not work.

It would be nice if someone could help me with the last step.

Thank you

Best regards,
Kate

August 25, 2016 at 1:50 pm

Use invoke-command with stop-process, it'll be easier for you.

The wmi method would be terminate. Really important to understand methods and properties of objects when learning powershell. Do not turn objects into custom objects with the select statement(you will lose the methods if you do). Don't use |where |where, use -and.

Get-WmiObject win32_process -computername AAA | select -first 1 |gm

August 25, 2016 at 1:52 pm

btw, if you google 'get-wmiobject stop process' you'll have tons of resources that explain how to do this.

https://blogs.technet.microsoft.com/heyscriptingguy/2011/08/08/learn-four-ways-to-kill-a-process-using-powershell-and-wmi/

August 26, 2016 at 5:01 am

Hello Dan,

thank you for your response.

Thanks to your input I got it – I still had to play around a little but this seems to work now:

Get-WmiObject win32_process -computername AAA -filter "name='lync.exe'" | Where-Object {$_.getowner().user -ne "BBB"} | Invoke-WmiMethod -name terminate

Thank you so much for your help 🙂

Best regards,
Kate