Home > Blockchain >  Powershell - How do I add multiple URLs to an Invoke-Webrequest
Powershell - How do I add multiple URLs to an Invoke-Webrequest

Time:04-09

I'm trying to download multiple PDFs from URLs using invoke-webrequest. I also want the PDFs to have unique names (that I choose) but to be downloaded into a single folder.

This is what I've got so far (for 1 url): invoke-webrequest -uri "https://www.website.com/fact.pdf" -Outfile $env:TEMPC:\Users\MyPC\Downloads\TEST.pdf

PS; this is literally my first time using powershell and do not have a clue

CodePudding user response:

Organize your URLs and their target file names in a hashtable:

$FilesToDownload = @{
    'TEST.pdf' = 'https://www.website.com/fact.pdf'
    'ANOTHER.pdf' = 'https://other.website.com/somethingElse.pdf'
    <# and so on ... #>
}

Now you just need to repeat the Invoke-WebRequest for each entry in the hashtable:

foreach($file in $FilesToDownload.GetEnumerator()){
    Write-Host "Downloading '$($file.Name)' from '$($file.Value)'"

    # construct output path, then download file
    $outputPath = Join-Path C:\Users\MyPC\Downloads -ChildPath $file.Name
    Invoke-WebRequest -Uri $file.Value -OutFile $outputPath
}
  • Related