Is it possible to multithread or run a script on hundreds of computers at the same time

How to multithread in powershell

I have found using foreach loops in powershell does the task well enough but when it comes to working with many remote servers and computers I need a much faster approach as this can only process one computer at a time, is it possible to multithread a script

Answer

This is one method in which you can create a new job for each computer in a list, there is a far more refined way than this, if you are interested click the read more button below.
The first job is to pipe from a list, you can use a csv or import them straight from AD.
Next we create a script-block which will execute on ever computer in the list.
The start-job will take care of the multithreading as the script fires the command and loop onto the next cycle of the foreach loop.
Finally we have a loop which will check on the jobs and keep the script active until they have all been processed

$computers = get-adcomputer -filter * | % {

### script###
$ScriptBlock = {
param($pipelinePassIn)
remove-item -path C:\test.txt
}

### multithread ###
Start-Job $ScriptBlock -ArgumentList $_
}



### Loop-until-jobs-finished ###
While (Get-Job -State "Running")
{
Start-Sleep 5
write-host "Jobs in progress - please wait" -foreground color red
}

Comments
// Collect comments ''
Search