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?