Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

PowerShell and Process Owners

Posted on February 28, 2019 by Alan

If you are running as an administrator, it is easy to get the owner associated with a process using PowerShell: Get-Process -IncludeUserName. Get-Process gets information from System.Diagnostics.Process, not WMI. But you can get similar information from WMI, even if you aren’t an administrator. We had a little debate about this in our last Charlotte PowerShell Meetup. Here is my solution to the issue:

<#
.Synopsis
   Add the process owner information to WMI Win32_Process object
.DESCRIPTION
   This advanced function adds the process owners User Domain and User Name to a Win32_Process object.  
   It is does not change the default display for the object, so you have to use Select-Object to see in output.
   You must "dot source" this prior to use 
.PARAMETER ProcessObject
A WMI Process object, ex: Get-CimInstance -classname Win32_Process
.EXAMPLE
Get-CimInstance -classname Win32_Process -Filter "ProcessID = $pid" |Get-ProcessOwner |  
select-object ProcessId, Name, UserDomain, UserName
Find the PID, Process name, and owner information for this instance of PowerShell
.EXAMPLE
Get-CimInstance -classname Win32_Process  |Get-ProcessOwner |  
select-object ProcessId, Name, UserDomain, UserName | Out-GridView
Find the PID, Process name, and owner information for all running processes, display in out-gridview
.NOTES
Alan Kaplan 2/8/2019 for the Charlotte PowerShell User Group MeetUp.
This does not work with Get-Process which comes from System.Diagnostics.Process, not WMI
Get-Process supports return of Username when running as an administrator with Get-Process -IncludeUserName
See https://powershelladministrator.com/tag/get-session-id/
Based on former member Ed Wilson's post:
https://blogs.technet.microsoft.com/heyscriptingguy/2015/02/27/~
get-process-owner-and-other-info-with-wmi-and-powershell/
#>
function Get-ProcessOwner {
    [CmdletBinding()]
    Param
    (  # Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Process
        [Parameter(Mandatory = $true, 
            ValueFromPipeline = $true)]
        $ProcessObject
    )
    Begin { }
    Process {
        Try {
            $o = Invoke-CimMethod -InputObject $ProcessObject -MethodName GetOwner -ErrorAction Stop 
            $DomUser = $o.Domain
            $NameUser = $o.User
        }
        Catch {
            $o = $null
        }
        $_ |    add-member -NotePropertyName 'UserName' -NotePropertyValue $NameUser -PassThru |
            add-member -NotePropertyName 'UserDomain' -NotePropertyValue $DomUser -PassThru
    }
    End { }
}

This function uses the GetOwner method of Win32_Process, and adds the user domain and user SamAccountName to the object in the pipeline.

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy