Killing a process on a local computer is easy.
1 |
(Get-Process -Name notepad2).Kill() |
Or:
1 |
Stop-Process -Name notepad2 |
Or:
1 |
Get-Process -Name notepad2 | Stop-Process |
You get the idea …
But neither of these work on a remote computer!
The stop-process
cmdlet doesn’t support the -ComputerName
switch, so the second method doesn’t even work remotely. The third method fails with an error Stop-Process : Feature is not supported for remote machines
and the first method fails with an error Exception calling "Kill" with "0" argument(s): "Feature is not supported for remote machines."
.
So it’s back to our old friend WMI for killing processes on remote computers. The objects returned via WMI contain a Terminate
method which we can make use of:
1 |
(Get-WmiObject Win32_Process -ComputerName mango | ?{ $_.ProcessName -match "notepad" }).Terminate() |
The snippet above terminates all processes with the name “notepad” on the remote computer “mango”.
If you are curious about what other methods are available when dealing with the Process
class using WMI, do the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
PS> gwmi win32_process | ?{ $_.ProcessName -match "notepad" } | gm -MemberType Method TypeName: System.Management.ManagementObject#rootcimv2Win32_Process Name MemberType Definition ---- ---------- ---------- AttachDebugger Method System.Management.ManagementBaseObject AttachDebugger() GetOwner Method System.Management.ManagementBaseObject GetOwner() GetOwnerSid Method System.Management.ManagementBaseObject GetOwnerSid() SetPriority Method System.Management.ManagementBaseObject SetPriority(System.Int32 Priority) Terminate Method System.Management.ManagementBaseObject Terminate(System.UInt32 Reason) |