get-process and UserName
Hi,
I'm trying to list all/some process and need to include the username since the script has to run in a terminal server environment. It works using WMI (
Get-WmiObject Win32_Process and GetOwner()
) but get-process provides some more attributes I need to export to a logfile.
Accoring to MSDN there is a property called UserName in the StartInfo object, but the value is always empty, when executing this script:
get-process
outlook | select-object id, path, @{Name="UserName";Expression = {$_.StartInfo.UserName}}
What's wrong? I am fairly new to PS, so is this the correct way of reading that property?
Thanks, regards
February 15th, 2010 9:18am
I'll keep searching, but I wonder if this property is somehow broken... Every process on my system shows an empty username:
PS>get-process|foreach{$_.startinfo.username}
February 15th, 2010 12:28pm
Hi,
I think some attributes of startinfo is not used to query information, it is used to start a process with the information your provide. You can use startinfo.username and startinfo.password to run the process as another user.
To query user name, you can also use "Environmentvariables"
get-process outlook|foreach{$_.startinfo.environmentvariables}
Thanks.
February 16th, 2010 2:36am
Ok, this workaround is ok in general, but not working in a terminal server environment, the output is always the same user name.
February 16th, 2010 8:39am
Hi,
Yes, you’re right, environmentvariables are globe settings. Currently, the easiest solution is to combine get-process and Win32_process. Get User name from Win32_process and get other information from get-process.
Thanks.
February 18th, 2010 2:24am
Hi,
yes that would do the trick. How can I combine the output of those two queries (get-process and get-wmiobject) into one output (table-format)?
Unfortunately this is beyond my (soon to grow) PS knowledge.
Thank you!
February 18th, 2010 7:24am
Add whatever other columns you want:
$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
-
Proposed as answer by
Marco ShawModerator
Thursday, February 18, 2010 10:45 PM
-
Marked as answer by
Matzer J
Friday, February 19, 2010 7:59 AM
February 18th, 2010 2:39pm
Add whatever other columns you want:
$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
That's a cool way... I was originally thinking of trying to match up the process id between get-process and win32_process to get the username...
February 18th, 2010 10:46pm
Thanks. It fought me for a littl while. The process Id is an Int32, and if you try to use that as a hash table key without going to string first, it give you a very unhelpful error.
-
Marked as answer by
Mervyn ZhangModerator
Friday, February 19, 2010 8:18 AM
-
Unmarked as answer by
Marco ShawModerator
Friday, February 19, 2010 11:29 AM
February 19th, 2010 12:29am
Very cool solution, I added the owner and also the commandline property, working fine now.
Thanks for all your effort, regards
February 19th, 2010 8:01am
This is fantastic. My question is how i would take the same code but add an exclusion for the owner of the process. ( ie: delete all *.exe processes whose owners are not equal to "string = x,y,z" or "array[x,y,z]" )
For instance. We have an erp system that creats a *.exe file for each logged on user. Our problem is that we have users that do not properly log off the sytem thus resulting in a hanging session and max users reached errors.
At the current time i manually delete these sessions but would like setup an automated process to clear these sessions every evening. There are about 12 of these sessions that we do not want to delete as they are more of a system process that we wish
to keep running.
Any help or direction would be much appreciated!
January 17th, 2011 8:58pm
That works nicely. But how would you add the commandline to that?
$owners = @{};$CommandLine = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
gwmi win32_process |% {$CommandLine[$_.????] = $_.????().????}
get-process | select processname,CommandLine,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
Thanks Peter
August 27th, 2012 6:58am
Cool! I try and get in PS.
get-process | where {$_.cpu -gt 100} | select cpu,pm,vm,processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]
}} | ft -AutoSize

October 21st, 2012 2:40pm
Hey Victor,
Thanks, it helped me a lot.. I have two questions here..
1) when we use the command you gave, it gives all the values in bytes. can we get the result in MB or GB?
2) I need start time, end time and memory used within that process time.
Are both these things possible?
Chaitanya.
May 23rd, 2013 4:27pm
Hi,
Just to add some more details, I got until here, and it works fine(values are in bytes)
$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
get-process | where {$_.cpu -gt 100}| select ws,cpu,pm,vm,Id,@{l="StartTime";e={ get-date $($_.starttime) -f "dd/MM/yy HH.mm" }},@{l="Current Time";e={get-date}},@{l="Owner";e={$owners[$_.id.tostring()]}} | Format-Table -Property * -AutoSize | Out-String -Width
5096 | Out-File C:\system.csv
Here, I am not able to get memory used details..
Thanks,
Chaitanya.
May 24th, 2013 8:07am
This beats the Windows 4.0 method (get-process -includeusername), which complains about needing to run as elevated.
September 19th, 2013 8:14pm
Wonderful! I like this one too!
September 19th, 2013 8:15pm
Add whatever other columns you want:
$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
Using your code - here is my one-liner:
gwmi win32_process |select Name, @{l="User name";e={$_.getowner().user}}
In my case I needed count of processes by certain user:
(gwmi win32_process |where {$_.getowner().user -eq "jsmith" -and $_.name -eq"powershell.exe"}).count
-
Proposed as answer by
Charlie Dancoisne - SQL - SCCM - Powershell
Tuesday, March 18, 2014 5:50 PM
-
Unproposed as answer by
Charlie Dancoisne - SQL - SCCM - Powershell
Tuesday, March 18, 2014 5:50 PM
October 17th, 2013 6:20pm
Hi,
These are many possible solutions. Some more easy than others.
I propose the following solution by abandoning the cmdlet Get-Process for the benefit of Get-WmiObject:
Get-WmiObject -Query "Select * from Win32_Process where name = 'Outlook.exe'" |
Select Name, Handle, @{Label='Owner';Expression={$_.GetOwner().User}}
Best Regards,
Charly
March 18th, 2014 5:48pm
Nice.
March 25th, 2015 9:23am