What's the difference Select ID vs Stop-Process ID

Welcome Forums General PowerShell Q&A What's the difference Select ID vs Stop-Process ID

This topic contains 3 replies, has 3 voices, and was last updated by

Senior Moderator
5 days, 12 hours ago.

  • Author
  • #170170

    Topics: 6
    Replies: 9
    Points: 14
    Rank: Member

    I am trying to figure out the why in the following codes. A "why" explanation would be appreciated.

    When I run the following code, select-object displays the piped object ID.

    get-process | where {$_.name -eq 'ytd'} | select ID

    The stop-process ID works if the object ID from the above code is hardwired.

    Stop-Process ####

    If I run the following code, PowerShell returns an error

    get-process | where {$_.name -eq 'ytd'} | Stop-Process ID

    Of course, this following would work and I understood why (Stop-Process is piped the object).

    get-process | where {$_.name -eq 'ytd'} | Stop-Process

    The question is what's the difference between the Stop-Process ID vs Select-Object ID; the object is piped to both cmdlets.

  • #170182

    Topics: 23
    Replies: 665
    Points: 1,529
    Helping Hand
    Rank: Community Hero

    What's the error message? Stop-process is probably trying to get the ID from both the pipe and what follows it at the same time.

    • #170668

      Topics: 6
      Replies: 9
      Points: 14
      Rank: Member

      Recap the above post:

      get-process | where {$_.name -eq 'ytd'} | select ID # Select output the process ID number
      Stop-Process ID # Use the ID number returned from above, it worked 
      get-process | where {$_.name -eq 'ytd'} | Stop-Process # No ID, it killed the process - ID was not needed
      get-process | where {$_.name -eq 'ytd'} | Stop-Process ID # error (see below); 
      # Trying to understand the difference between line 2 Stop-Process ID and the piped Stop-Process ID

      I understood that ID after Stop-Process was not needed; however I was trying to understand why and how:

      get-process | where {$_.name -eq 'ytd'} | Stop-Process ID

      The following error was returned:

      Stop-Process : Cannot bind parameter 'InputObject'. Cannot convert the "ID" value of type
      "System.String" to type "System.Diagnostics.Process".
      At line:1 char:56
      + get-process | where {$_.name -eq 'ytd'} | Stop-Process ID
      +                                                        ~~
          + CategoryInfo          : InvalidArgument: (:) [Stop-Process], ParameterBindingException
          + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.StopProcess

      ID string was returned to Select-Object above and Stop-Process ID (integer) was parsed to successfully as well.

      Why when it was piped, Stop-Process looked for System.Diagnostic.Process data type as in the error message.

  • #170680

    Senior Moderator
    Topics: 8
    Replies: 1004
    Points: 3,255
    Helping Hand
    Rank: Community Hero


    when you use the Stop-Process cmdlet alone, then it requires an input to understand which process to stop(either ID or ProcessName), but when using the same cmdlet in a pipeline(chaining of cmdlets), then the required input is taken from pipeline and should not be passed (Get-Process -Name something | Stop-Process -Id $ID). It will understand what to consume from the pipeline object and proceed. Most of the builtin cmdlets in PowerShell has this capability.

    see more here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pipelines?view=powershell-5.1

You must be logged in to reply to this topic.