Hi, welcome to the forum
PowerShell In a Month of Lunches calls your first example the PowerShell or command-oriented approach and your second example ‘the scripting approach’.
The point here is that using
ForEach is often (but not always) an indicator that you’re taking a scripting approach rather than a pure PowerShell approach […] There are only two times when I find myself legitimately using
- When there’s no cmdlet capable of doing what I need to a bunch of objects at once. This most often happens when I need to execute a method against a bunch of objects, and there isn’t a cmdlet that can perform the equivalent task.
- When I need to manually ‘unwind’ a bunch of objects and send them off, one at a time, to a custom function that I’ve written, which can only work with one at a time.
Source: Learn Windows PowerShell in a Month of Lunches by Don Jones, p 226.
Bear in mind, for your example, you don’t actually need the pipeline at all:
Stop-Process -Name msedge,putty