The necessity of loops while using the pipeline

This topic contains 4 replies, has 4 voices, and was last updated by  js 1 week, 1 day ago.

  • Author
    Posts
  • #93291

    Garegin Asatryan
    Participant

    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?

  • #93303

    Don Jones
    Keymaster

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

  • #93328

    Garegin Asatryan
    Participant

    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.

  • #93352

    David Schmidtberger
    Participant

    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.

  • #93441

    js
    Participant

    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                                                                                               
    

You must be logged in to reply to this topic.