Difference between ForEach and ForEach-Object in powershell

前端 未结 4 580
失恋的感觉
失恋的感觉 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:55

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

    The foreach ($ in $){} syntax you are using is help about_foreach.

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

    The keyword foreach operates over each $ in the $ as given in the () bit.

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

提交回复
热议问题