Sunday, June 17, 2012

PowerShell - Memory usage per user

From time to time you might need to find out which user to logoff from a server because of low memory.

instead of looking at the Task Manager and trying to find who is using the most, you can use this PowerShell script:

$h = @{}
get-wmiobject win32_process | foreach {
    $u = $_.getowner().user;
    if ( $u -ne $null)
    {
        if ( !$h.ContainsKey($u)  )
        {
            $h.add( $u, $_.WS);
        }
        else
        {
            $h.item($u) = $h.item($u) + $_.WS;
        }
    }

$h.GetEnumerator() | sort value -desc
Notice that this gives you the actual RAM usage which might not be what you are looking for, as recent users probably using more RAM than not recent ones.

If you want to know the total usage of Virtual Memory - replace "WS" with "VM".