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

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 
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.