# Multithreading Script

**URL:** <https://forums.powershell.org/t/multithreading-script/15334>\
**Category:** PowerShell Help\
**Created:** [December 4, 2020, 6:32am UTC](https://forums.powershell.org/t/multithreading-script/15334 "2020-12-04T06:32:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph-monarch](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-monarch](https://forums.powershell.org/u/joseph-monarch)\
**Post date:** [December 4, 2020, 6:32am UTC](https://forums.powershell.org/t/multithreading-script/15334/1 "2020-12-04T06:32:59Z")

</div>

I am attempting to multithread a script and any time I increase the amount of threads, the performance of the script greatly decreases. For example, if I set a max of 5 threads I can write 635 objects/min. When I set to 10 threads, I can write only 361 objects/min. I have beefed up resources on the server I am running this from and never get close to maxing out the hardware, so I know it’s not a hardware limitation.

Here is the script:

```
$runspacecollection = @()
$runspacepool = [runspacefactory]::CreateRunspacePool(1, 5)
$runspacePool.Open()

$scriptblock = {
    param($folder)

    Script goes here

}

Write-Host ‘Parsing Folder Structure’
$folders = Get-ChildItem \\server\Datastore
Write-Host ‘Processing Files’

foreach ($folder in $folders) {
    $powershell = [powershell]::Create().AddScript($scriptblock).AddArgument($folder)
    $powershell.RunspacePool = $runspacepool
    [Collections.ArrayList]$runspacecollection += New-Object -TypeName System.Management.Automation.PSObject -Property @{
        runspace = $powershell.BeginInvoke()
        powershell = $powershell
    }
}

while ($runspacecollection) {
    foreach ($runspace in $runspacecollection.ToArray()) {
        if ($runspace.Runspace.IsCompleted) {
            $runspace.Powershell.EndInvoke($runspace.Runspace)
            $runspace.Powershell.Dispose()
            $runspacecollection.Remove($runspace)
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![joseph-monarch](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-monarch](https://forums.powershell.org/u/joseph-monarch)\
**Post date:** [December 4, 2020, 10:49am UTC](https://forums.powershell.org/t/multithreading-script/15334/2 "2020-12-04T10:49:58Z")

</div>

After checking various settings and syntax, my issue is with the server that I am reading files from. I can see that I am getting all the threads, however, I am being bottlenecked by the read speed on the server I am reading from.

The more threads the bigger the read speed bottleneck. Explains a lot.

---

<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:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/multithreading-script/15334/3 "2024-05-16T20:28:12Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


