PowerShell - Need to kill remote process using service name RRS feed

  • Question

  • Hello,

    I need to kill processes on remote computer using PowerShell. The known value is ServiceName. Please help!

    If I replaced 

    If ($id -gt 0) {Stop-Process $id}


    If ($id -gt 0) {Write-Host $id}

    It returned the values. However, when I tried to stop the process it generated error:

    Stop-Process : Cannot find a process with the process identifier 7644.
    At C:\PowerShell\KillScriptEngine.ps1:22 char:25
    +         If ($id -gt 0) {Stop-Process $id}
    +                         ~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (7644:Int32) [Stop-Process], ProcessCommandException
        + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.StopProcessCommand

    param (
    Foreach ($serv in $Computer)
        $ids = (Get-WmiObject -ComputerName $serv -Class Win32_Service  -Filter "Name LIKE 'SCRPTENG%'").ProcessID
      Foreach ($id in $ids)
            If ($id -gt 0) {Stop-Process $id}
    Thursday, September 29, 2016 1:30 AM


All replies

  • Get-Service -Name ServiceName -Computer $server | Stop-Service


    Thursday, September 29, 2016 1:47 AM
  • Thanks so much. I got myself into unnecessary complexity.

    Thursday, September 29, 2016 3:04 AM
  • The Get-Service -Name ServiceName -Computer $server | Stop-Service will stop the ServiceName specified in there. However, it won't stop the Service that stuck with 0 CPU. Even with Stop-Service -Force, it kept generate message like "WARNING: Waiting for service 'ServiceName' to stop..." Only way to stop that service is killing the PID by Stop-Process -Id PID. 

    However, when I ran the script that posted prior, it could write-host with full 5 service PIDs but when I tried to use Stop-Process for those 5, it said "Cannot bind argument to parameter 'Id' because it is null." Please help. Thanks so much

    Tuesday, October 4, 2016 1:20 PM
  • Stop-Process is trying to kill processes on your local machine, not the remote.

    Tuesday, October 4, 2016 1:27 PM
  • If I issue with specific process not using variable

    Invoke-Command -ComputerName servername -ScriptBlock {Stop-Process -ID 4436} 

    it will kill the process remote. But in the script it won't pass the variable to the Stop-Process even though variable were there as Write-Host could display it. If you said Stop-Process is trying to kill processes on local machine not the remote then how can you kill the stuck remote process after you found out from the servicename? Thanks.

    • Edited by N0oblet Tuesday, October 4, 2016 2:15 PM
    Tuesday, October 4, 2016 2:14 PM
  • Use -ArgumentList to pass local variables to the remote machine:

    Tuesday, October 4, 2016 2:19 PM
  • I am not quite understand. The variables obtained from the remote machine, it became local variables? Sorry for my noob. Can you please explain a bit more? Thanks so much.
    Tuesday, October 4, 2016 2:22 PM
  • Stop-Service does not allow remote.  You must use Get-WmiObject

    -=:Kill Process By Name:=-

    (Get-WmiObject Win32_Process -ComputerName $ComputerName | ?{ $_.ProcessName -match "$ProcessName" }).Terminate()

    -=:Kill Process By ID:=-

    (Get-WmiObject Win32_Process -ComputerName $ComputerName | ?{ $_.ProcessId -match $ProcessID }).Terminate()

    Wednesday, March 7, 2018 7:37 PM
  • Get-Service spooler -Computer remotepc | Stop-Service

    Remote works just fine.  Get-Service gets a service controller object which can ,anage local and remote services.


    $svc = Get-Service spooler -Computer remotepc


    Wednesday, March 7, 2018 9:09 PM