Kill process remotely using PowerShell function

Today’s post is about how to kill process remotely using PowerShell script. You probably had this situation when some process was consuming 99% of memory or cpu resources and server was working very slowly or was unresponsive. As a temporary solution you can kill process remotely using this function and then logon to machine and do further troubleshooting.

After executing this function you will be asked to select processes from pop-up window – in this example it will show you processes on local machine:

kill process
kill process

To do that we will use Out-GridView with PassThru parameter:

$Processes = Get-Process -ComputerName $Server | Out-GridView -PassThru -Title "Select processes"
      

Then you will be asked to confirm that you want to proceed and kill selected processes. In this example I killed notepad process:

Kill Process
Kill Process

Function is just based on simple command taskkill:

TASKKILL /s $Server /f /IM $Process.id

Final script:


Function Kill-Process {
    [CmdletBinding()]        
  
    # Parameters used in this function
    param
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Provide server name", ValueFromPipeline = $true)] 
        $Server = $env:computername
    ) 
        # Gathering processes
        Try{
            $Processes = Get-Process -ComputerName $Server -ErrorAction Stop | Select-Object ProcessName,@{n='Mem (KB)';e={[int]($_.WS/1KB)}},@{n='CPU';e={[int]($_.CPU/1KB)}},ID  | Sort-Object 'Mem (KB)' -Descending | Out-GridView -PassThru -Title "Select processes"
        }
        Catch{
            $_.Exception.Message
            Break
        }
 
    If($Processes){
        Write-Host "The following processes have been selected:" -ForegroundColor Yellow
        $Processes | Out-String
    
        # Confirm if you want to proceed
        Write-Host -NoNewline "Do you want to proceed? (Y/N): "
        $Response = Read-Host
  
            # If response was different that Y script will end
            If( $Response -ne "Y" ){ 
                Write-Warning "Script ends"     
            }
            Else{
                $ErrorActionPreference = "Stop"
                ForEach($Process in $Processes){
                    Write-Host "`nProcess" $Process.processname "on $Server :" -ForegroundColor Green
                    Try{
                        TASKKILL /s $Server /f /IM $Process.id
                        # Stop-Process -Id $Process.id -Force -Verbose # You can use this for killing process on localhost
                    }
                    Catch{
                        $_.Exception.Message
                        Continue
                    }
                }
            }
     }
     Else{
        Write-Warning "Script ends: Processes have not been selected"
     }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.