# Foreach-Object -Parallel Counter

**URL:** <https://forums.powershell.org/t/foreach-object-parallel-counter/20305>\
**Category:** PowerShell Help\
**Created:** [August 29, 2022, 9:52pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305 "2022-08-29T21:52:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbk5us](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mbk5us/32/2684_2.png) [@mbk5us](https://forums.powershell.org/u/mbk5us)\
**Post date:** [August 29, 2022, 9:52pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/1 "2022-08-29T21:52:16Z")

</div>

Hi,  
How to a use a counter variable inside for the foreach-object -parallel?  
Something like:  
$i=0  
1…10 | ForEach-Object -parallel {$I++; Write-host $i}

It does not work. What’s wrong?  
Thanks.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [August 30, 2022, 12:22am UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/3 "2022-08-30T00:22:00Z")

</div>

> [@mbk5us](#):
>
> 1…10 | ForEach-Object -parallel {$I++; Write-host $i}

Check your version of Powershell. The documentation claims -Parallel was introduced in PS 7.

> **[ForEach-Object (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2)**
>
> The ForEach-Object cmdlet performs an operation on each item in a collection of input objects. The input objects can be piped to the cmdlet or specified using the InputObject parameter. Starting in Windows PowerShell 3.0, there are two different ways...

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 30, 2022, 3:04am UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/4 "2022-08-30T03:04:59Z")

</div>

Assuming your on the correct version of powershell… ‘Does not work’ does not tell us anything. Do you get errors?

---

<div class="post-metadata">

**Author:** ![mbk5us](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mbk5us/32/2684_2.png) [@mbk5us](https://forums.powershell.org/u/mbk5us)\
**Post date:** [August 31, 2022, 2:56am UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/5 "2022-08-31T02:56:48Z")

</div>

I defined the $i=0 prior to the foreach and the value does not change in the loop.  
I am using POSH v7.2.6  
Thanks.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 31, 2022, 3:59am UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/6 "2022-08-31T03:59:06Z")

</div>

The variable you define outside of the foreach-object will not be known in the runspaces. They are like remote sessions, variables can be referenced with `$using:` modifier, but afaik can’t be updated. The object you pass in (in this case `1..10`) will be available to the runspace as `$_`

```auto
$i = 48
1..10 | ForEach-Object -parallel {
    Write-Host "This number won't change $($using:i)"
    Write-Host "But this number is passed in $_" -ForegroundColor Cyan
}

```

The reason you were seeing the number 1 output previously is a blank variable with ++ is equal to 1. You can try it without runspaces. Make up a variable and watch

```auto
$doesntexist++
Write-Host $doesntexist

```

In each loop you were basically defining local `$i = 1`

---

<div class="post-metadata">

**Author:** ![mbk5us](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mbk5us/32/2684_2.png) [@mbk5us](https://forums.powershell.org/u/mbk5us)\
**Post date:** [August 31, 2022, 6:28pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/7 "2022-08-31T18:28:37Z")

</div>

Great! We are getting closer.  
What I really need is to have a counter inside the foreach loop when I open a file and go thru it in the loop and the counter will show me the progress.  
Like:  
$i=0  
Get-content MyFile.txt | ForEach-Object - Parallel {$\_ ;Write-host $i}  
Thanks.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [August 31, 2022, 7:01pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/8 "2022-08-31T19:01:19Z")

</div>

Might want to have a look at Write-Progress

> **[Write-Progress (Microsoft.PowerShell.Utility) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-progress?view=powershell-7.2)**
>
> The Write-Progress cmdlet displays a progress bar in a PowerShell command window that depicts the status of a running command or script. You can select the indicators that the bar reflects and the text that appears above and below the progress bar....

---

<div class="post-metadata">

**Author:** ![mbk5us](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mbk5us/32/2684_2.png) [@mbk5us](https://forums.powershell.org/u/mbk5us)\
**Post date:** [August 31, 2022, 8:49pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/9 "2022-08-31T20:49:36Z")

</div>

Great idea but the issue persists with using write-progress: a counter needs to be increased within that foreach-object -Parallel loop.

---

<div class="post-metadata">

**Author:** ![mbk5us](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/mbk5us/32/2684_2.png) [@mbk5us](https://forums.powershell.org/u/mbk5us)\
**Post date:** [September 2, 2022, 9:08pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/10 "2022-09-02T21:08:19Z")

</div>

I think I found the secret for using “imported” variables inside a ForEach-Object - Parallel loop at: [ForEach-Object (Microsoft.PowerShell.Core) - PowerShell | Microsoft Docs](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.2) - Example # 14

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 2, 2022, 11:01pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/11 "2022-09-02T23:01:50Z")

</div>

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

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

$hash.I = 0

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

```

![image](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/0/0f07be4964ff54ad66ad2713f2335ed4bd54c574.png)

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [April 8, 2024, 7:51pm UTC](https://forums.powershell.org/t/foreach-object-parallel-counter/20305/12 "2024-04-08T19:51:35Z")

</div>

Closed on April 8, 2024, 7:51pm UTC


