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