Skip to main content

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.

# Download multiple files at the same time
#
# 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.
#
# https://learn.microsoft.com/en-us/powershell/module/threadjob/start-threadjob?view=powershell-7.4#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
}