PoshCode Archive  Artifact Content

Artifact 3b0b510e993705b0570cc65a07d60f3e7587c122097869719c98ffea034352c5:

  • File Terminate-process-user.ps1 — part of check-in [be72d2dda1] at 2018-06-10 13:29:30 on branch trunk — #Ty Lopes – Calgary – Oct 2012 (user: Ty Lopes size: 1450)

# 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