Help me please in writing a script - find a user with more processes running powershell.
I tried this option, but I'm not sure if the result is correct:
ps -IncludeUserName|? UserName -m "$env:USERNAME"
CodePudding user response:
You can take the process objects you receive from Get-Process
and use Group-Object
to group them by UserName. Then sort the groups you get back by the Count property and Select only the last object which will be the group with the highest count of processes. Supplying -ExpandProperty Name
will return only the Name property of the group which contains the UserName (which is what we grouped on using Group-Object
)
Get-Process -IncludeUserName |
Group-Object UserName |
Sort-Object Count |
Select-Object -Last 1 -ExpandProperty Name