23 July 2015

Extract links froma web site and download multiple files with Poweshell Script

Few weeks ago, I've posted about how to download multiple files from web using powershell. In this post I'm going to show you a script that retrieves the links from a given web site and downloads the files to a given directory on your local machine.

#place to store downloaded files
$filepath = Read-Host "Please enter the location to store the files.(eg. C:\Downloads\)"
$webSite = Read-Host "Please enter the site url that contains the links: (eg. https://www.example.com/photos)"
#gathering link details from source web
$links = ((Invoke-WebRequest Uri $webSite).Links)
foreach($link in $links) {
#link to files
$item = $webSite + $link.href
Write-Host Url: $item
# innertext contains the file name
Write-Host Title:  $link.innerText
#download path on local machine
$path = $filepath + $link.innerText
Write-Host $path
#downloading files
Start-BitsTransfer $item  $path