Powershell ForEach-Object : Cannot bind parameter

powershell

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

Best Answer

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