HOW TO download a list of files with a PowerShell script

If you use Windows Vista or higher, PowerShell comes along with it. So if you have to download a list of files, you can do that easily with this PowerShell script instead of installing any other tools (command-line or GUI)
# PowerShell script to download a list of files that have a predictable number pattern
for ($i=1; $i -le 10; $i++)
{
$startTime = (Get-Date)
# Download files that have the naming format File1.txt, File2.txt etc..
(new-object System.Net.WebClient).DownloadFile("http://example/file$i.txt","C:\temp\file$i.txt")
$endTime = (Get-Date)
# Echo Time taken
"Time taken to download file$i : $(($endTime-$startTime).totalseconds) seconds"
}
view raw PSDownloadFiles hosted with ❤ by GitHub


To get a single file, you can use just this line of code:
(new-object System.Net.WebClient).DownloadFile('http://www.xyz.net/file.txt', 'C:\tmp\file.tx‌​t')
You can also use the PowerShell Invoke-WebRequest cmdlet:
# PowerShell script to download a list of files that have a predictable number pattern 1.jpg, 2.jpg etc..
$baseUri = "https://somesite.com/files/"
for ($i = 1; $i -lt 9; $i++) {
{
$f = $baseUri + $i + ".jpg"
$Downloadpath = "C:\temp\" + $i + ".jpg"
Write-Output "Processing file: $f"
Invoke-WebRequest -Uri $f -Outfile $Downloadpath
}
view raw download.ps hosted with ❤ by GitHub

Comments

Popular posts from this blog

The Mercurial Grok AI Assistant Understands & Speaks Indian Languages

Things Near Me – Find & Learn About Landmarks Nearby