Author Posts

February 8, 2018 at 4:04 pm

My vague understanding is that powershell processes the objects in a pipeline one by one. So get-process gets all the processes and stop-process stops them serially. You don't need to do looping.
While in other cases I see the for loop being used. For example, to get all unused features and remove them from disk. Is this a limitation of the nature of different objects?

February 8, 2018 at 5:41 pm

Basically, yes. If you don't have a command capable of doing the looping internally, then you have to do it yourself.

February 8, 2018 at 8:04 pm

Got it. One “downside” of powershell is that it does lot of magic in connecting different objects to commands. In other shells it's just stream of text. But trace-command is my friend in this case.

February 9, 2018 at 2:36 pm

I don't think its really "Magic", at least once you understand how it selects properties of objects from the pipeline. Every command's help will tell you how those specific properties are taken by any command.

The Learn Windows Powershell in a Month of Lunches book (Chapter 9) does a pretty amazing job of explaining the entire process.

It can seem like magic, or sometimes even guessing on powershell's part for how it pulls these things together. But once you get a base understanding, it makes quite a lot of sense. You just need to ensure you understand what type of objects you are manipulating, and what is and is not accepted by the cmdlets themselves via pipeline.

February 11, 2018 at 5:12 pm

Usually if a parameter can be piped in, the receiving cmdlet has a process block that will take the place of a loop.

[pscustomobject]@{name='pwsh','terminal'} | get-process

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName                                                                                            
 ------    -----      -----     ------      --  -- -----------                                                                                            
      0     0.00      66.53     471.04    8988 411 pwsh                                                                                                   
      0     0.00      81.40   2,089.89    7410   1 Terminal                                                                                               

Some parameters can take an array, so you wouldn't need a loop either. It varies.

get-process -name pwsh,terminal

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName                                                                                            
 ------    -----      -----     ------      --  -- -----------                                                                                            
      0     0.00      66.54     474.87    8988 411 pwsh                                                                                                   
      0     0.00      81.40   2,119.72    7410   1 Terminal