Mar 052018
 

The PowerShell Get-Process commandlet cannot return the process owner in the v3 implementation. In PowerShell v4, the Get-Process cmdlet now has a “-IncludeUserName” parameter but it will only run as elevated (administrator).

I’ve been searching for an optimized script, running in a non-elevated context. But all I’ve found was slow and high resource demanding scripts to match or find a process owner. I needed a simple, short and efficient script to run in Citrix XenApp environments.

So I’m happy to share with you my “optimized” version of the script:

$proc = @(Get-CimInstance -Query "Select * from Win32_Process where name = 'explorer.exe'")
for ($i=0; $i -lt $proc.length; $i++){
    $owner = ($proc[$i] | Invoke-CimMethod -MethodName GetOwner).User
    if ($owner -eq $env:UserName){
        $processid = ($proc[$i]).ProcessId
        Stop-Process -Id $processid}}

This example runs in the user context, find the explorer.exe owned by the user and restart it by stopping the process (explorer.exe will automatically restart when stopped via a PS script). I need to restart explorer.exe if the user shell folders (folder redirections) have changed since the last logon.

But this script could be reused to find the owner of a high CPU usage process (I’m thinking at some PDF readers stuck at 99% CPU usage when the user is disconnected). Finding the process owner is not easy with the current version of PowerShell, but you can use my own script freely if you like it.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.