Avatar of sara2000
sara2000
 asked on

Top ten CPU usage by usename and apps

We have a terminal server that has been accessed by a few users. I would like to know which user's process takes more CPU.  Sysadmin out there must be using a simple PS script to gather the top 10 CPU process consumed by end-users, I will appreciate if you could share with me.  I use below command at present, but can not e-mail to group of users.
Get-Process -IncludeUserName | Sort-Object CPU -desc | Select -first 5| Select ProcessName,ID,CPU,UserName,Description | Format-Table -AutoSize
Powershell

Avatar of undefined
Last Comment
sara2000

8/22/2022 - Mon
oBdA

Not quite sure what you're actually looking for.
The command to get the processes won't be getting shorter/less complex than this.
You can remove  one of the Select-Objects, but will have to add a Where-Object filter to only show end users.
Get-Process -IncludeUserName | ? {$_.UserName -like "${env:UserDomain}\*"} | Sort CPU -desc | Select ProcessName, ID, CPU, UserName, Description -First 10 | Format-Table -AutoSize

Open in new window

sara2000

ASKER
Thanks, oBda,
This is what I want, how do I get this info fro remote server?
oBdA

Do you want to deploy this as a one-liner for copy and paste into a PS console, or rather as a full script?
If as a script, do you want to hard-code the server name(s) and edit as required, or do you want to pass it as command line argument?
Your help has saved me hundreds of hours of internet surfing.
fblack61
sara2000

ASKER
I want to put this in a script and hardcode the server, but I will edit the server name if I need it for another server.
oBdA

Try this then:
$serverName = 'SomeServer'
Invoke-Command -ComputerName $serverName -ScriptBlock {
	Get-Process -IncludeUserName |
		Where-Object {$_.UserName -like "${env:UserDomain}\*"} |
		Sort-Object -Property CPU -Descending |
		Select-Object -Property ProcessName, ID, CPU, UserName, Description -First 10
} | Format-Table -AutoSize -Property PSComputerName, ProcessName, ID, @{n='CPU'; e={$_.CPU.ToString('N2')}; a='Right'}, UserName, Description

Open in new window

sara2000

ASKER
Thank you for this help.
I removed the Format-table and inserted convert-html to output to a HTML instaed, the HTML file is listing all the info I want in addition I have extra columns like PScomputername, runspaceid etc.
How do I avoid that from that HTML file?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sara2000

ASKER
Thank you very much, it is working like the way I wanted !!