Foreach-Object -Parallel Counter

Ahh very good, you’ve jogged my memory. Here’s a slightly simpler example than 14

$hash = [hashtable]::Synchronized(@{})

$hash.I = 0

1..10 | ForEach-Object -parallel {
    $hash = $using:hash
    $hash.I++
    
    Write-host $hash.I
}

image

The trick is to assign the synchronized variable to a local variable and then it will handle updating the original across threads.