Top memory processes with their owner
Win32_Process gives the memory in use (WorkingSetSize, in bytes) but not the owner account: it comes from the GetOwner method, called with Invoke-CimMethod.
$process = Get-CimInstance -ClassName Win32_Process |
Sort-Object -Property WorkingSetSize -Descending |
Select-Object -First 10
foreach ($item in $process) {
$owner = Invoke-CimMethod -InputObject $item -MethodName GetOwner
$ownerName = $null
if ($owner.ReturnValue -eq 0) {
$ownerName = '{0}\{1}' -f $owner.Domain, $owner.User
}
[PSCustomObject]@{
Name = $item.Name
ProcessId = $item.ProcessId
WorkingSetMB = [math]::Round($item.WorkingSetSize / 1MB)
Owner = $ownerName
}
}
GetOwner returns ReturnValue = 0 on success; for some system processes it fails without an elevated console, and the Owner column stays empty. WorkingSetSize is the physical memory of the process: a total per program name requires grouping the rows.
Variant: remote
The foreach loop stays the same: the method call goes through the session of the instance.
$cimSession = New-CimSession -ComputerName 'SERVER01'
try {
$cimParameters = @{
CimSession = $cimSession
ClassName = 'Win32_Process'
}
$process = Get-CimInstance @cimParameters |
Sort-Object -Property WorkingSetSize -Descending |
Select-Object -First 10
# Same foreach loop as above: GetOwner runs on the server
} finally {
Remove-CimSession -CimSession $cimSession
}