Difference between ForEach and ForEach-Object in powershell

前端 未结 4 484
失恋的感觉
失恋的感觉 2020-12-01 06:09

Is there any difference between ForEach and ForEach-Object ?

I have a small code like this, works fine

$txt = Get-Content \         


        
相关标签:
4条回答
  • 2020-12-01 06:47

    They're different commands for different purposes. The ForEach-Object cmdlet is used in the pipeline, and you use either $PSItem or $_ to refer to the current object in order to run a {scriptblock} like so:

    1..5 | ForEach-Object {$_}
    
    >1
    >2
    >3
    >4
    >5
    

    Now, you can also use a very similiar looking keyword, ForEach, at the beginning of a line. In this case, you can run a {scriptblock} in which you define the variable name, like this:

    ForEach ($number in 1..5){$number}
    >1
    >2
    >3
    >4
    >5
    

    The core difference here is where you use the command, one is used in the midst of a pipeline, while the other starts its own pipeline. In production style scripts, I'd recommend using the ForEach keyword instead of the cmdlet.

    0 讨论(0)
  • 2020-12-01 06:49

    Apart from the technical differences that have been mentioned before, here are some practical differences, for sake of completeness (see also):

    1.) In a pipeline, you do not know the total count of the processed items. This is a situation where you would opt to acquire the complete list first and then do a foreach-loop.

    Example:

    $files = gci "c:\fakepath"
    $i = 0
    foreach ($file in $files) {
        $i++
        Write-Host "$i / $($files.Count) processed"
    }
    

    2.) With an existing list, the foreach loop is faster than then pipeline version, because the script block does not have to be invoked each time. (But the difference might be negligible depending on the work you do and the number of items.)

    Example:

    $items = 0..100000
    Measure-Command { $items | ForEach-Object { $_ } }
    # ~500ms on my machine
    Measure-Command { foreach ($i in $items) { $i } }
    # ~70ms on my machine
    
    0 讨论(0)
  • 2020-12-01 06:55

    foreach is an alias of ForEach-Object but it appears to also be a keyword (which is confusing).

    The foreach ($<item> in $<collection\>){<statement list>} syntax you are using is help about_foreach.

    The foreach as ForEach-Object alias is help ForEach-Object.

    The keyword foreach operates over each $<item> in the $<collection> as given in the () bit.

    The alias foreach/function ForEach-Object operates over each item of the collection it receives as input.

    0 讨论(0)
  • 2020-12-01 07:07

    Both the previous answers are correct, but https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/08/getting-to-know-foreach-and-foreach-object/ has both a good summary:

    When you are piping input into ForEach, it is the alias for ForEach-Object. But when you place ForEach at the beginning of the line, it is a Windows PowerShell statement.

    and more details:

    The ForEach statement loads all of the items up front into a collection before processing them one at a time. ForEach-Object expects the items to be streamed via the pipeline, thus lowering the memory requirements, but at the same time, taking a performance hit.

    He then includes some performance measurements and concludes:

    So which one do you use? Well, the answer is, “It depends.” You can iterate through a collection of items by using either the ForEach statement or the ForEach-Object cmdlet. ForEach is perfect if you have plenty of memory, want the best performance, and do not care about passing the output to another command via the pipeline. ForEach-Object (with its aliases % and ForEach) take input from the pipeline. Although it is slower to process everything, it gives you the benefit of Begin, Process, and End blocks. In addition, it allows you to stream the objects to another command via the pipeline. In the end, use the approach that best fits your requirement and the capability of your system.

    0 讨论(0)
提交回复
热议问题