Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - get-process & stop-process by owner

Reply
 
Old 03-03-2007   #1 (permalink)
Andrew Conrad


 
 

get-process & stop-process by owner

I'm trying to figure out how I can list and stop only those processes that
are being run by a specific user like you can with tasklist and taskkill.

In otherwords what is the translation of these commands:

tasklist /fi "username eq drevlan"
taskkill /fi "username eq drevlan" /im calc.exe

In powershell using get-process and stop-process.

Thank you!

--Andrew


My System SpecsSystem Spec
Old 03-03-2007   #2 (permalink)
Brandon Shell


 
 

Re: get-process & stop-process by owner

I would have thought the object returned from get-process would have a user
account, but I couldnt find it (I could have missed it.) Either way... I
used wmi.

I wrote this script... its a quicky and no promises I did test it and it
worked for me... I added the -whatif switch just in case.

code
====
function Kill-UserProcess{
param([string]$server,[string]$user,[string]$process,[switch]$Kill,[switch]$whatif) if($server){$processes = Get-WmiObject Win32_Process -ComputerName $server} else{$processes = Get-WmiObject Win32_Process} if($user){ if($kill){if(!$process){Write-Host "Killing all Processes for User[$user]"}} foreach($p in $processes){ if($p.GetOwner().user -match "$user"){ if($process){ if($p.Name -match "$process"){ if($kill){ Write-Host "Killing Process [$($p.Name)] for User [$user]" if($whatif){ write-Host "What if: Performing operation `"kill`" on Target`"$($p.Name)`"." } } else{ Write-Host "Killing Process $($p.Name)" $p.Terminate() | out-null } return $true } } if($kill){ if($whatif){ write-Host "What if: Performing operation `"kill`" on Target`"$($p.Name)`"." } else{ Write-Host "Killing Process $($p.Name)" $p.Terminate() | out-Null } } else{ Write-Host "$p.Name" } } } }}"Andrew Conrad" <drevlan@gmail.com> wrote in messagenews:78BD196E-0B80-4649-A457-5B9DDAB7850B@microsoft.com...> I'm trying to figure out how I can list and stop only those processes thatare being run by a specific user like you can with tasklist and taskkill.>> In otherwords what is the translation of these commands:>> tasklist /fi "username eq drevlan"> taskkill /fi "username eq drevlan" /im calc.exe>> In powershell using get-process and stop-process.>> Thank you!>> --Andrew
My System SpecsSystem Spec
Old 03-03-2007   #3 (permalink)
Brandon Shell


 
 

Re: get-process & stop-process by owner

Alright... that didn't turn out so well... I posted on my blog
http://bsonposh.com/modules/wordpress/?p=20

"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:ueF9pFaXHHA.1240@TK2MSFTNGP04.phx.gbl...
>I would have thought the object returned from get-process would have a user
>account, but I couldnt find it (I could have missed it.) Either way... I
>used wmi.
>
> I wrote this script... its a quicky and no promises I did test it and
> it worked for me... I added the -whatif switch just in case.
>
> code
> ====
> function Kill-UserProcess{
> param([string]$server,[string]$user,[string]$process,[switch]$Kill,[switch]$whatif)
> if($server){$processes = Get-WmiObject Win32_Process -ComputerName
> $server} else{$processes = Get-WmiObject Win32_Process} if($user){
> if($kill){if(!$process){Write-Host "Killing all Processes for
> User[$user]"}} foreach($p in $processes){ if($p.GetOwner().user -match
> "$user"){ if($process){ if($p.Name -match "$process"){
> if($kill){ Write-Host "Killing Process [$($p.Name)] for User
> [$user]" if($whatif){ write-Host "What if: Performing
> operation `"kill`" on Target`"$($p.Name)`"." } }
> else{ Write-Host "Killing Process $($p.Name)" $p.Terminate() |
> out-null } return $true } } if($kill){
> if($whatif){ write-Host "What if: Performing operation `"kill`" on
> Target`"$($p.Name)`"." } else{ Write-Host "Killing Process
> $($p.Name)" $p.Terminate() | out-Null } } else{
> Write-Host "$p.Name" } } } }}"Andrew Conrad" <drevlan@gmail.com>
> wrote in
> messagenews:78BD196E-0B80-4649-A457-5B9DDAB7850B@microsoft.com...> I'm
> trying to figure out how I can list and stop only those processes thatare
> being run by a specific user like you can with tasklist and taskkill.>> In
> otherwords what is the translation of these commands:>> tasklist /fi
> "username eq drevlan"> taskkill /fi "username eq drevlan" /im calc.exe>>
> In powershell using get-process and stop-process.>> Thank you!>> --Andrew


My System SpecsSystem Spec
Old 03-03-2007   #4 (permalink)
Andrew Conrad


 
 

Re: get-process & stop-process by owner

Amazing! Thank you so much Brandon you're a superstar!

--Andrew


"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:%23NfybVaXHHA.3984@TK2MSFTNGP02.phx.gbl...
> Alright... that didn't turn out so well... I posted on my blog
> http://bsonposh.com/modules/wordpress/?p=20
>
> "Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
> news:ueF9pFaXHHA.1240@TK2MSFTNGP04.phx.gbl...
>>I would have thought the object returned from get-process would have a
>>user account, but I couldnt find it (I could have missed it.) Either
>>way... I used wmi.
>>
>> I wrote this script... its a quicky and no promises I did test it and
>> it worked for me... I added the -whatif switch just in case.
>>
>> code
>> ====
>> function Kill-UserProcess{
>> param([string]$server,[string]$user,[string]$process,[switch]$Kill,[switch]$whatif)
>> if($server){$processes = Get-WmiObject Win32_Process -ComputerName
>> $server} else{$processes = Get-WmiObject Win32_Process} if($user){
>> if($kill){if(!$process){Write-Host "Killing all Processes for
>> User[$user]"}} foreach($p in $processes){ if($p.GetOwner().user -match
>> "$user"){ if($process){ if($p.Name -match "$process"){
>> if($kill){ Write-Host "Killing Process [$($p.Name)] for User
>> [$user]" if($whatif){ write-Host "What if: Performing
>> operation `"kill`" on Target`"$($p.Name)`"." } } else{
>> Write-Host "Killing Process $($p.Name)" $p.Terminate() |
>> ull } return $true } } if($kill){ if($whatif){
>> write-Host "What if: Performing operation `"kill`" on
>> Target`"$($p.Name)`"." } else{ Write-Host "Killing Process
>> $($p.Name)" $p.Terminate() | out-Null } } else{ Write-Host
>> "$p.Name" } } } }}"Andrew Conrad" <drevlan@gmail.com> wrote in
>> messagenews:78BD196E-0B80-4649-A457-5B9DDAB7850B@microsoft.com...> I'm
>> trying to figure out how I can list and stop only those processes thatare
>> being run by a specific user like you can with tasklist and taskkill.>>
>> In otherwords what is the translation of these commands:>> tasklist /fi
>> "username eq drevlan"> taskkill /fi "username eq drevlan" /im calc.exe>>
>> In powershell using get-process and stop-process.>> Thank you!>> --Andrew

>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to stop process? Vista General
Failover Guest Cluster -- 'The process cannot access the file becauseit is being used by another process.' Virtual Server
Process wlmail.exe doesn't stop...very often Live Mail
get the owner of a process PowerShell
Bug? Shouldn't Stop-Process automatically match Id if object is a process? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46