Don't understand why it won't work

Simple but don’t understand

If I type

$tot =0

gci |%{$tot += $_.length}

It will return value

$tot = gci |%{$tot += %_.length}

It return nothing.

why ?

Hmmm … for me

$tot = 0
Get-ChildItem |
ForEach-Object{
$tot += $_.length
}

… does not return anything … if you meant it fills the variable $tot with the cumulated sizes of the files of the current directory - yes it does that.

But what should “%_” be? What do you expect it to do? Don’t you get an error (a lot of errors?)

REgardless of the syntax , it won’t return anything. The scriptblock should spit something for the $tot variable to catch. In this case its always caught in $tot inside the foreach statement.

As @Olaf-Soyk mentioned, didn’t you get any errors with that syntax ?