PowerShell Script: Script Processes like Task Manager (Name, CPU Percentage, Memory, Disk)

I had a question on StackOverflow about showing CPU percentage. Using the
Get-WmiObject cmdlet with the -class switch and the option

Win32_PerfFormattedData_PerfProc_Process

I came up with this function that does the trick.

Function Get-CPUProcess
{
$properties=@(
    @{Name="Name"; Expression = {$_.name}},
    @{Name="PID"; Expression = {$_.IDProcess}},
    @{Name="CPU (%)"; Expression = {$_.PercentProcessorTime}},
    @{Name="Memory (MB)"; Expression = {[Math]::Round(($_.workingSetPrivate / 1mb),2)}}
    @{Name="Disk (MB)"; Expression = {[Math]::Round(($_.IODataOperationsPersec / 1mb),2)}}
)
$ProcessCPU = Get-WmiObject -class Win32_PerfFormattedData_PerfProc_Process |
    Select-Object $properties |
    Sort-Object "CPU (%)" -desc |
    Select-Object -First 5 |
    Format-Table -AutoSize
    $ProcessCPU
}
Get-CPUProcess
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s