Do you use the .Foreach() and .where() operators?

Welcome Forums General PowerShell Q&A Do you use the .Foreach() and .where() operators?

Viewing 5 reply threads
  • Author
    Posts
    • #242780
      Participant
      Topics: 6
      Replies: 15
      Points: 141
      Rank: Participant

      Do you use the .foreach() and .where() operators? I find them very convenient for assigning the output of the foreach construct to a variable.

      $Output = $Array.where{FilterExpression}.foreach{ActionExpression}

      Being able to chain them like methods makes it very easy for me to follow. Especially when I was first learning. They really made the light bulb go off for how functions interact with each other.

      Additionally, for console base Administration it makes single line execution terse and convenient.

      $Array.where{FilterExpression}.id is functionally easier for me to type/tab complete through than ($array | where {FilterExpression}).id, which if you are like me and forget to type an opening parenthesis a lot, it can be a frustrating and regularly wasted keystroke. It also conveniently outputs to a Generic Collection, which you can add and remove items from. I personally don’t write code that finds much use for this ever, but it is a thing.

      I’m just curious if anyone else has anything to say about the benefits or drawbacks other than differences in execution time.

      Alternatively please convince me not to use these anymore if I shouldn’t 😅.

      • This topic was modified 2 months, 1 week ago by Phatmandrake.
      • This topic was modified 2 months, 1 week ago by Phatmandrake.
    • #242798
      Participant
      Topics: 6
      Replies: 16
      Points: 111
      Rank: Participant

      I’ll give these try in sequence like you demonstrated.  I remember reading that the .foreach() method is more efficient then the ForEach-Object cmdlet.

      Thanks for sharing.

    • #242807
      Participant
      Topics: 8
      Replies: 558
      Points: 2,136
      Helping Hand
      Rank: Community Hero

      As Thomas found out on another thread, the where method has a useful “split” mode. Each of these have their place. But as you also found out, Sean, there are sometimes minor differences or things that don’t work as expected.

    • #246735
      Participant
      Topics: 12
      Replies: 38
      Points: 261
      Helping Hand
      Rank: Contributor

      Late to the party, sorry. This looks interesting, and I thought I’d run it through Measure-Command

      First I created an array: $Tags = 1..199 | foreach {"Tag$_"}

      Then I measured how long it took:
      Measure-Command {$Tags|ForEach{$_}}
      This took TotalMilliseconds : 12.8882

      And Measure-Command {$Tags.ForEach{$_}} which took TotalMilliseconds : 13.9521.

      Upping it to $Tags = 0..1999999 | foreach {"Tag$_"} the results are different:
      Measure-Command {$Tags|ForEach{$_}} is Milliseconds      : 316
      Measure-Command {$Tags.ForEach{$_}} is Milliseconds      : 276

      Obviously, use what you’re comfortable with, but if you are writing something that takes a while, it’s worth checking how long commands take to run.

    • #247020
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,688
      Helping Hand
      Rank: Community Hero

      I only use them occasionally. I like the ‘until’ feature of the where method. Foreach and where remind me of map and filter in javascript (but no reduce).

      [/crayon]
      • This reply was modified 1 month, 2 weeks ago by js.
      • This reply was modified 1 month, 2 weeks ago by js.
    • #247276
      Participant
      Topics: 0
      Replies: 78
      Points: 348
      Helping Hand
      Rank: Contributor

      Foreach() and  Where() methods are more concise and more performant for many cases.  Where() method offers different modes for data filtering and more iteration control. I have personally seen some object types that have issues with these methods even when those objects were singular or part of an array. But those same objects have no issues with  Foreach-Object or Where-Object.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.