0

I've got a PowerShell function that's returning a list of executable names (with the file extension), and I'm trying to kill any of these if they are running, but not having much success. Here's the command I'm using:

Get-Executable-Names `
| where { $_ -match ".exe" } `
| foreach { $_ -replace ".exe" } `
| foreach { ps $_ } `
| kill

If I store the output of Get-Executable-Names in a variable and display its contents, it shows up as:

Path
----
A.exe
B.exe
C.exe

PowerShell is reporting this error:

Get-Process : Cannot find a process with the name "@{Path=A}". Verify the process name and call the cmdlet again.
+ $Get-Executable-Names | where { $_ -match ".exe" } | foreach { $_ -replace ".exe" } | foreach { ps <<<< $_ } | kill
+ CategoryInfo : ObjectNotFound: (@{Path=A}:String) [Get-Process], ProcessCommandException

It seems that the -replace operation changes the pipe data to the following format:

@(Path=A)
@(Path=B)
@(Path=C)

which I don't understand. I'm sure I'm just misunderstanding PowerShell's object model here, but what am I overlooking?

Joey
  • 344,408
  • 85
  • 689
  • 683
arathorn
  • 2,098
  • 3
  • 20
  • 29

1 Answers1

2

Try adding the following just after the call to GetExecutableNames

%{ $_.Path }

Full answer

Get-Executable-Names 
| where { $_ -match ".exe" } 
| %{ $_.Path }
| %{ $_ -replace ".exe" } 
| %{ ps $_ } 
| kill
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • To explain what was going on, Get-Executable-Names (you should remove the last dash) is returning an array of hashtables. When you convert a hashtable to a string (which is needed for -replace), you get the code that represents the hashtable. – JasonMArcher Feb 28 '10 at 03:53