Skip to main content

Download multiple files at the same time using PowerShell and the Start-ThreadJob cmdlet.

##
# Download multiple files at the same time using the Start-ThreadJob cmdlet
#
# The Invoke-WebRequest cmdlet can only download one file at a time. The
# following example uses Start-ThreadJob to create multiple thread jobs to
# download multiple files at the same time.
#
# Start-ThreadJob creates background jobs similar to the Start-Job cmdlet. The
# main difference is that the jobs which are created run in separate threads
# within the local process. By default, the jobs use the current working
# directory of the caller that started the job.
#
# The cmdlet also supports a ThrottleLimit parameter to limit the number of jobs
# running at one time. As more jobs are started, they are queued and wait until
# the current number of jobs drops below the throttle limit.
#
#
# Source: https://docs.microsoft.com/powershell/module/threadjob/start-threadjob?view=powershell-7.2#example-5-download-multiple-files-at-the-same-time
##

$baseUri = 'https://github.com/PowerShell/PowerShell/releases/download'
$files = @(
    @{
        Uri = "$baseUri/v7.3.0-preview.5/PowerShell-7.3.0-preview.5-win-x64.msi"
        OutFile = 'PowerShell-7.3.0-preview.5-win-x64.msi'
    },
    @{
        Uri = "$baseUri/v7.3.0-preview.5/PowerShell-7.3.0-preview.5-win-x64.zip"
        OutFile = 'PowerShell-7.3.0-preview.5-win-x64.zip'
    },
    @{
        Uri = "$baseUri/v7.2.5/PowerShell-7.2.5-win-x64.msi"
        OutFile = 'PowerShell-7.2.5-win-x64.msi'
    },
    @{
        Uri = "$baseUri/v7.2.5/PowerShell-7.2.5-win-x64.zip"
        OutFile = 'PowerShell-7.2.5-win-x64.zip'
    }
)

$jobs = @()

foreach ($file in $files) {
    $jobs += Start-ThreadJob -Name $file.OutFile -ScriptBlock {
        $params = $using:file
        Invoke-WebRequest @params
    }
}

Write-Host "Downloads started..."
Wait-Job -Job $jobs

foreach ($job in $jobs) {
    Receive-Job -Job $job
}