header image

Win32_Process examples: get process owner

Posted by: | January 15, 2014 | No Comment |

Moving on with examples of using Win32_Process you can find the process owner:

function get-procowner {
[CmdletBinding()]
param (
[string]$computername = $env:COMPUTERNAME
)

Get-CimInstance -ClassName Win32_Process -ComputerName $computername |
foreach {
  $owner = Invoke-CimMethod -InputObject $psitem -MethodName GetOwner

  $props = [ordered]@{
             Name = $psitem.Name
             Domain = $owner.Domain
             User = $owner.User
             ComputerName = $psitem.PSComputerName
          
           }
  New-Object -TypeName PSObject -Property $props
}
}

Use the same param block as before to pass a computername – defaulted to local machine.

For each of the Win32_Process objects get the corresponding owner using the GetOwner method.

Create an ordered hash table for the properties and output a PSObject using those properties

under: PowerShell and CIM, PowerShell and WMI, PowerShell V3, PowerShell v4