PowerShell for Windows Admins

Jan 14 2014   12:50PM GMT

Win32_Process examples–processor time and memory usage

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I saw some example code for using Win32_Process and didn’t like it so decided to create my own versions.  In this case the objective is to display the processor time and memory usage:

function get-proctimeandRAM {
[CmdletBinding()]
param (
[string]$computername = $env:COMPUTERNAME
)

Get-CimInstance -ClassName Win32_Process -ComputerName $computername |
foreach {
$props = [ordered]@{
Name =  $psitem.Name
ProcessId = $psitem.ProcessId
WorkingSetSize = $psitem.WorkingSetSize
PageFileUsage = $psitem.PageFileUsage
PageFaults = $psitem.PageFaults
ProcessorTime = ($psitem.KernalModeTime + $psitem.UserModeTime) / 10000000
}

New-Object -TypeName PSObject -Property $props
}
}

Start by creating an advanced function that takes a computername as a parameter.  This is used to call Get-CimInstance to access the Win32_Process class on the machine.

I’m switching to the CIM cmdlets for everything as my test environment is, or soon will be, Windows 2012 R2 or Windows 8.1.

For each if the Win32_Process objects create a New-Object. I’ve chosen to use an ordered hash table (PS 3 and above) so that my properties remain in the order I want

The function produces a list by default as there 6 properties on the object. If you want  table output use Format-Table.

Examples of use:

get-proctimeandRAM
get-proctimeandRAM | ft -AutoSize

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: