2

I am trying to get the owner of a process using this code:

(Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe'}).getowner() | Foreach-Object user | out-string

This works great under Windows 8 but in Windows 7 I get this message:

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the "user" value of type "System.String" to type "System.Management.Automation.ScriptBlock". At C:\Program Files (x86)\Advanced Monitoring Agent GP\scripts\9660.ps1:1 char: 108 + (Get-WmiObject -class win32_process | where{$_.ProcessName -eq 'explorer.exe' }).getowner() | Foreach-Object <<<< user | out-string + CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter BindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

jscott
  • 24,484
  • 8
  • 79
  • 100
user3460112
  • 21
  • 1
  • 2

2 Answers2

2

Please modify the user iterator and try on both 7 and 8:

(Get-WmiObject -class win32_process | 
    where{$_.ProcessName -eq 'explorer.exe'}).getowner() | 
    Foreach-Object {$_.user } | out-string 
kmarsh
  • 3,103
  • 16
  • 22
Raf
  • 308
  • 1
  • 8
2

You can do this with line breaks:

gwmi win32_process | where ProcessName -Match "explorer" | foreach {$_.GetOwner().User | Out-String}

Or without

gwmi win32_process | where ProcessName -Match "explorer" | foreach {$_.GetOwner().User}

Remember to wrap your foreach 'script' inside {}'s.

For the sake of completeness I'll say this was done with Powershell 3.0, hence no {} for the where-object cmdlet, and no $_ for the ProcessName property.

MDMoore313
  • 5,581
  • 6
  • 36
  • 75