Get Process Owners

One way to find out the owner of a process is to add the missing Owner property to process objects:

$processes = Get-WmiObject Win32_Process -Filter "name='notepad.exe'"

$appendedprocesses = foreach ($process in $processes) {
Add-Member -MemberType NoteProperty -Name Owner -Value (
$process.GetOwner().User) -InputObject $process -PassThru }

$appendedprocesses | ft name, owner

A loop adds the necessary Owner property. The resulting objects now show the owner. You could then use the information in Owner to selectively stop all processes owned by a specific user.

Twitter This Tip! ReTweet this Tip!


Posted Dec 17 2009, 08:00 by ps1

Comments

LucD wrote re: Get Process Owners
on 12-17-2009 11:34

Great script.

Btw comments seem to be working ;-)

Ben wrote re: Get Process Owners
on 12-17-2009 11:49

Ah. It helps to be logged in. ;)

I too like the script. I started modding it by making it a one-liner but ended up wanting to use this in my profile as an everyday tool so I went the other direction and expanded the script instead.

function Get-PSOwner($searchString)

{

   $foundProcess = ps $searchString

   if($foundProcess -eq $null) { return; }

   gwmi Win32_Process -Filter ("Handle={0}" -f $foundProcess.id ) |

       % { Add-Member `

           -InputObject $_ `

           -MemberType NoteProperty `

           -Name Owner `

           -Value ($_.GetOwner().User) `

           -PassThru } |

       select Name, Handle, Owner

}

Aleksandar wrote re: Get Process Owners
on 12-17-2009 1:01 PM

Your nice little function won't work if you get more than one process, and you will get an error if it doesn't find any, so I've added "-ea silentlycontinue" part and piped $foundprocess to "gwmi" script block:

function Get-PSOwner($searchString)

{

  $foundProcess = ps $searchString -ea silentlycontinue

  if($foundProcess -eq $null) { return; }

$foundprocess | % {

  gwmi Win32_Process -Filter ("Handle={0}" -f $_.id ) |

      % { Add-Member `

          -InputObject $_ `

          -MemberType NoteProperty `

          -Name Owner `

          -Value ($_.GetOwner().User) `

          -PassThru } |

      select Name, Handle, Owner

}

}

Usage:

Get-PSOwner explorer

Get-PSOwner exp*

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.