0
votes

I know that working code is

Get-Process firefo* | Stop-Process

But my first guess was

Get-Process | findstr firefox | Stop-Process

It didn't work.

Stop-Process : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:33
+ Get-Process | findstr firefox | Stop-Process
+                                 ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (   1379     317...               :PSObject) [Stop-Process], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand

I understand that string

   1342     306  1228412    1279864 -1671 ...71,42  35912 firefox

is bad for process killing, but why?

PS C:\Users\adamg> Get-Process firefo*

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
   1342     306  1228412    1279864 -1671 ...71,42  35912 firefox

The above works just fine, even with column headers in reply.

1
You have to look at Get-Help -full for the cmdlets and look at the Input and Output object. The dos command findstr only produces console text as output which cannot be used as input for a Powershell cmdlet. - user4317867

1 Answers

3
votes

findstr is a commandline utility that produces string output. Get-Process outputs Process objects, which is what Stop-Process expects as input. It could also handle a list of process IDs, but it can't parse the formatted string from findstr.

In PowerShell you normally wouldn't use findstr anyway. Use a Where-Object filter instead:

Get-Process | Where-Object { $_.ProcessName -like '*firefox*' } | Stop-Process