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: Wikipedia Viewer

Demo: Wikipedia Viewer HTML Code: <body><div class="container"> <h1>Wikipedia Search</h1> <p>A Free Code Camp Intermediate Project</p><br/>…

PowerShell: Adding a New Domain Controller

Error when trying to run DCPROMO on a Windows 2019 Server: ---------------------------Active Directory Domain Services…

PowerShell: Quick Snippet to Remove Virtual Machine Snapshots in VMM

$vmNames=@( 'MACHINE1', 'MACHINE2' ) foreach($vmName in $vmNames){ $checkpoint = Get-SCVMCheckpoint -VM $vmName if($checkpoint){$checkpoint|%{Remove-SCVMCheckpoint -VMCheckpoint $_…