Win32_Process examples–processor time and memory usage

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 {
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 | ft -AutoSize

This entry was posted in CIM, PowerShell and WMI, PowerShell V3, PowerShell v4. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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