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 Answers 2

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.