Anand Vaidya
Anand Vaidya

Reputation: 649

Get username for PID (ProcessId)

I have a PID for which I want to check its username. I knew that we can use GetOwner(), but it is the valid method for Get-WmiObject Win32_Process. I am using Get-WmiObject -Class Win32_PerfRawData_PerfProc_Process in which there is no way to get username (as per I search online). So, I think to check PID separately is the only way to resolve this.

Can you please tell me how can I get the username of PID or get username inside Win32_PerfRawData_PerfProc_Process?

Upvotes: 0

Views: 2137

Answers (1)

StefTheo
StefTheo

Reputation: 449

As it is described in this technet article :Technet you can use the code below.

In the last line you can put the process you want after the get-process command.

e.g. Get-Process outlook | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}

$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
Get-Process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}

The time it takes depends on how many services are currently running. Your output will be like:

ProcessName      Id         Owner
-----------      --         -----
OUTLOOK          13128      UserName

Hope that helps. Kind regards.

Upvotes: 3

Related Questions