iChill
iChill

Reputation: 21

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

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

EDIT: Thanks - was exactly what I was after.

I am wondering if you would be able to help me further. I've to download these PDFs from these URLs on a daily basis. I would already have the previous day's PDFs downloaded, so am now looking to see if I could run a script where:

  1. create an 'archive' folder and move previous day's pdfs (which would be located in different folders) into said folder
  2. to have unique names for new PDFs (you have already shown me this using hashtable)
  3. to download PDFs from the URLs, but place them in different locations e.g. 'TEST.pdf' to go to C:\Users\MyPC\Downloads\PDFs\Short and 'ANOTHER.pdf' to go to C:\Users\MyPC\Downloads\PDFs\Long and so on...

Upvotes: 1

Views: 1382

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

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
}

Upvotes: 4

Related Questions