Link to home
Start Free TrialLog in
Avatar of 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
Avatar of 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

Avatar of sara2000


Thanks, oBda,
This is what I want, how do I get this info fro remote server?
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?
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.
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

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?
Avatar of oBdA

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much, it is working like the way I wanted !!