1

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"
5
  • It's not clear what you are looking to do. You want to find the user with the most processes? Commented Sep 26, 2021 at 19:33
  • Yes! i want to find the user with the most processes Commented Sep 26, 2021 at 19:35
  • Is ps really an alias for Get-Process? You should be able to use Group-Object as a quick solution to this question. Also, the quotes around $env:username are not needed; although it is a good practice. Commented Sep 26, 2021 at 19:43
  • What is -m? You can't shorten -match. Commented Sep 26, 2021 at 19:51
  • @js2010 You cannot shorten -match in expression mode, but you can in argument mode, because with the simplified syntax being being used, the operators are simply (switch) parameters, and abbreviating parameter names works just fine, as long as the abbreviation is unambiguous. That said, the usual recommendation applies: use abbreviations only interactively, not in scripts. Commented Sep 26, 2021 at 22:30

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.