# Multi Threads

**URL:** <https://forums.powershell.org/t/multi-threads/12620>\
**Category:** PowerShell Help\
**Created:** [May 26, 2019, 8:06pm UTC](https://forums.powershell.org/t/multi-threads/12620 "2019-05-26T20:06:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xen](https://avatars.discourse-cdn.com/v4/letter/x/c77e96/32.png) [@xen](https://forums.powershell.org/u/xen)\
**Post date:** [May 26, 2019, 8:06pm UTC](https://forums.powershell.org/t/multi-threads/12620/1 "2019-05-26T20:06:14Z")

</div>

Hello,

&nbsp;

i have a Script for Install Security Hotfixes remote.The Script is great, but if you add a lot of hosts it take a long time to finish the deployment. Have anyone a idea how i can add multi threads?

Here is the code:

```
$RootHotfixPath = 'C:\Temp\'

$Hotfixes = @('windows6.1-kb4499175-x64_3704acfff45ddf163d8049683d5a3b75e49b58cb.msu')
$Servers = Get-Content 'C:\temp\MachineList.txt'

foreach ($Server in $Servers)
{
Write-Host "Processing $Server..."

$needsReboot = $False
$remotePath = "\\$Server\c$\Temp\Patches\"

if( ! (Test-Connection $Server -Count 1 -Quiet))
{
Write-Warning "$Server is not accessible"
continue
}

if(!(Test-Path $remotePath))
{
New-Item -ItemType Directory -Force -Path $remotePath | Out-Null
}

foreach ($Hotfix in $Hotfixes)
{
Write-Host "`thotfix: $Hotfix"
$HotfixPath = "$RootHotfixPath$Hotfix"

Copy-Item $Hotfixpath $remotePath
# Run command as SYSTEM via PsExec (-s switch)
& C:\Tools\PSTools\PsExec -s \\$Server wusa C:\Temp\Patches\$Hotfix /quiet /norestart
write-host "& C:\Tools\PSTools\PsExec -s \\$Server wusa C:\Temp\Patches\$Hotfix /quiet /norestart"
if ($LastExitCode -eq 3010) {
$needsReboot = $true
}
}

# Delete local copy of update packages
Remove-Item $remotePath -Force -Recurse

if($needsReboot)
{
Write-Host "Restarting $Server..."
#Restart-Computer -ComputerName $Server -Force -Confirm
}
}
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [May 26, 2019, 8:13pm UTC](https://forums.powershell.org/t/multi-threads/12620/2 "2019-05-26T20:13:44Z")

</div>

Yup, because you are doing it sequentially. Psexec do have an option to read the computer list from a file. You could use that option.

Example

```
psexec @C:\TEMP\serverlist.csv -u UrDomain\Username -c c:\file.exe
```

---

<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:32pm UTC](https://forums.powershell.org/t/multi-threads/12620/3 "2024-05-16T20:32:42Z")

</div>


