Posted On October 7, 2019

PowerShell: Kill Processes Remotely

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Kill Processes Remotely
    # This function requires 2 parameters: -computerName and -processName
function killRemoteProcess{
[cmdletbinding()]
param(
[string]$computerName=$env:COMPUTERNAME,
[parameter(Mandatory=$true)]
[string]$processName="powershell.exe"
)

# WMI querying method
$processes = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter "name='$processName'"

if ($processes){
foreach ($process in $processes) {
$terminationResult = $process.terminate()
$processid = $process.handle

if($terminationResult.returnvalue -eq 0) {
write-host "The process $ProcessName `($processid`) terminated successfully"
} else {
write-host "The process $ProcessName `($processid`) termination has some problems"
}
}
}else{
"$processName not found. Try again ya."
}
}

Sample OutPut

PS C:\Windows\system32> killRemoteProcess -computername $computer -ProcessName powershell.exe
The process powershell.exe (5880) terminated successfully

Leave a Reply

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

Related Post

JavaScript: Build a Tic Tac Toe Game (without AI)

Demo: https://blog.kimconnect.com/wp-content/projects/ticTacToeGame.html CSS Code: @import url('https://fonts.googleapis.com/css?family=Merienda');body{ font-family: 'Merienda', cursive; font-weight: bold;}#gameBoard { width: 396px; //…

Hyper-V Servers Report

# hyperVServersReport.ps1 # Version: 0.0.1 # Description: # This script will scan for Hyper-V Hosts…

How to Set Default UTF-8 encoding for New Notepad Documents When Saving File

Part 1: Set Default UTF-8 encoding for New Notepad Documents When Saving File for Legacy…