Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List process for current user

Tags:

powershell

As an administrator I can get a users processes by running this

Get-Process -IncludeUserName | Where UserName -match test

But as a non-administrator I can't use -IncludeUserName becuase "The 'IncludeUserName' parameter requires elevated user rights".

So if I'm logged on as the user test, how do I list only his processes and not everything that's running?

like image 260
PatricF Avatar asked Feb 04 '16 07:02

PatricF


People also ask

How do I list all processes for a specific user?

Open the terminal window or app. To see only the processes owned by a specific user on Linux run: ps -u {USERNAME} Search for a Linux process by name run: pgrep -u {USERNAME} {processName} Another option to list processes by name is to run either top -U {userName} or htop -u {userName} commands.

How do I find the process username?

Process Name and User Name: Before PowerShell 4.0 PS C:\WINDOWS\system32> Get-WmiObject -Class Win32_Process -Filter "name='calculator.exe'" | Foreach {$_. GetOwner() | Get-Member} TypeName: System. Management.


Video Answer


4 Answers

This is faster, one line, doesn't require admin.

Get-Process | ? {$_.SI -eq (Get-Process -PID $PID).SessionId}

like image 70
Alex Kwitny Avatar answered Sep 28 '22 19:09

Alex Kwitny


You can do that through WMI. Here is an excerpt of an article you can find here.

$View = @(
 @{l='Handles';e={$_.HandleCount}},
 @{l='NPM(K)';e={ (Get-Process -Id $_.ProcessId).NonpagedSystemMemorySize/1KB -as [int]}},
 @{l='PM(K)';e={ $_.PrivatePageCount/1KB -as [int]}},
 @{l='WS(K)';e={ $_.WorkingSetSize/1KB -as [int]}},
 @{l='VM(M)';e={ $_.VirtualSize/1mB -as [int]}},
 @{l='CPU(s)';e={ (Get-Process -Id $_.ProcessId).CPU -as [int]}},
 @{l='Id';e={ $_.ProcessId}},
 'UserName'
 @{l='ProcessName';e={ $_.ProcessName}}
)
Get-WmiObject Win32_Process | % { $_ | 
    Add-Member -MemberType ScriptProperty -Name UserName -Value {
        '{0}\{1}' -f $this.GetOwner().Domain,$this.GetOwner().User
    } -Force -PassThru
} | ? UserName -match $env:USERNAME | ft $View -AutoSize
like image 35
David Brabant Avatar answered Sep 28 '22 19:09

David Brabant


Get-Process alone will not give you this information, you'll need WMI:

$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
$ps = get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
foreach($p in $ps) {
    if($p.Owner -eq $env:USERNAME) {
        $p
    }
}
like image 30
Marc Avatar answered Sep 28 '22 21:09

Marc


Thanks for your code. Based on this, I created a modified version to allow users to kill (a subset of) their own processes:

#Script to allow users to kill (a subset of) their own processes 
#Based on : https://stackoverflow.com/questions/35195221/list-process-for-current-user
#Previously we used Task Nanny created by Michel Stevelmans which is a lot faster, but it did not show a process that was causing issues for our users.
$UserProcesses = @()
$Owners = @{}
Get-WmiObject win32_process | Foreach{$owners[$_.handle] = $_.getowner().user}
$Processes = Get-Process | select processname,Description,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
Foreach($Process in $Processes)
{
    IF($process.Owner -eq $env:USERNAME)
    {
        $UserProcesses += $Process
    }
}
$UserProcessesToExclude = @(
    'concentr', #Citrix Connection Center
    'conhost', #Console Window Host
    'dwm', #Desktop Windows Manager
    'explorer', #Explorer
    'Receiver', #Citrix Receiver Application
    'rundll32', #Windows host process (Rundll32)
    'ssonsvr', #Citrix  Receiver 
    'taskhost' #Host Process for Windows Tasks
    'wfcrun32' #Citrix Connection Manager
    'wfshell' #Citrix wfshell shell
)

$UserProcesses | Where{$_.ProcessName -notin $UserProcessesToExclude} | Out-GridView -Title 'Task killer - Select the process(es) you want to kill. Hold CTRL to select multiple processes.' -PassThru | Foreach{Stop-Process -id $_.Id}
like image 37
user3735573 Avatar answered Sep 28 '22 19:09

user3735573