Re:“Why I develop on Windows”
by cyberbanjo on Friday April 28, 2023 @07:32PM
(#35748467)
I'm still a PowerShell novice, but I believe ForEach-Object and ForEach are subtly different, and PowerShell has classic For(Init; Condition; Increment) loops also but it's even more different than those two.
[ Parent
| on HN ]
Re:“Why I develop on Windows”
by deafpolygon on Saturday April 29, 2023 @10:20AM
(#35753105)
ForEach-Object and ForEach are the same thing.
Get-Help ForEach-Object
...
ALIASES foreach %
Re:“Why I develop on Windows”
by cyberbanjo on Saturday April 29, 2023 @03:49PM
(#35756479)
I have seen that, and that's partially why it seemed so confusing to me but at least one difference is
The ForEach-Object cmdlet works much like the Foreach statement, except that you can't pipe input to a Foreach statement. For more information about the Foreach statement, see about_Foreach.
https://learn.microsoft.com/en-us/powershell/module/microsof...And another appears to be performance
PS /home/me> $time = (Measure-Command {
>> 1..1E4 | ForEach-Object {
>> $_
>> }
>> }).TotalMilliseconds
PS /home/me> [pscustomobject]@{
>> Type = 'ForEach-Object'
>> Time_ms = $Time
>> }
Type Time_ms
---- -------
ForEach-Object 144.558
PS /home/me> $Time = (Measure-Command {
>> ForEach ($i in (1..1E4)) {
>> $i
>> }
>> }).TotalMilliseconds
PS /home/me> [pscustomobject]@{
>> Type = 'ForEach_Statement'
>> Time_ms = $Time
>> }
Type Time_ms
---- -------
ForEach_Statement 17.621
https://devblogs.microsoft.com/scripting/getting-to-know-for...
Re:“Why I develop on Windows”
by deafpolygon on Sunday April 30, 2023 @07:56AM
(#35761750)
Hey TIL, though, I knew there was a difference (the foreach statement is meant to be typed all lower-cased, since it is not a cmdlet but really a shell built-in but PowerShell is case-insensitive). Get-Help gets you information on cmdlets, not shell built-ins.
Though I was referring to the cmdlet, you're absolutely correct.