# encoding: ascii
# api: powershell
# title: Terminate process / user
# description: #Ty Lopes – Calgary – Oct 2012
# version: 0.1
# type: script
# author: Ty Lopes
# license: CC0
# x-poshcode-id: 3689
# x-archived: 2012-10-14T23:37:29
# x-published: 2012-10-12T08:11:00
#
# #(Troy is a huge nerd)
# #How to kill a process by username
# #Originally created for a script (running under a sched task) that was not closing excel application after enumerating throught the excel file
# #Powershell does not seem to close excel properly using the workbook.close function
# #Note: could not kill the process when I included the extension… example notepad.exe has to be notepad for the $process variable
#
#Ty Lopes - Calgary - Oct 2012
#(Troy is a huge nerd)

#How to kill a process by username
#Originally created for a script (running under a sched task) that was not closing excel application after enumerating throught the excel file
#Powershell does not seem to close excel properly using the workbook.close function
#Note: could not kill the process when I included the extension... example notepad.exe has to be notepad for the $process variable

$username = "username"
$process= "notepad"

$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
get-process $process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}} | where-object {$_.owner -eq $username} | kill -force
