Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Download file(s) from URL that does not include file name in the URL

I've searched around and tried a few things and have not gotten it to work. The link would be for example: http://www.website com/file/[fileID] (e.g. http://www.website com/file/1wA5fT) and a box would appear whether to save the file(s) or not.

I have tried this, from what I can remember and it did not work.

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads\"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

Edit:

I am able to correctly download the file if I put a filename for the destination. However I need to extract the filename from e.g.

<a href="http://www.website.com/file/[fileID]">Filename.txt</a></li></div></ul>

After I get this singled out how would I single out the filename into $Filename?

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads\$Filename"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

This code would work then.

like image 483
Charles Feemster Avatar asked Oct 08 '12 21:10

Charles Feemster


People also ask

Can PowerShell download files from a URL?

You can use PowerShell to download single or multiple files from the internet. There are a couple of methods when it comes to downloading files with PowerShell. We can download files from any URL with PowerShell, local network shares, and from behind credential protected websites.

Can PowerShell download files?

PowerShell users can use the Invoke-Webrequest, New-Object, or Start-BitsTransfer cmdlets to download files.


2 Answers

$source = "http://www.website.com/file/someFile.txt"
$Filename = [System.IO.Path]::GetFileName($source)
$dest = "C:\Users\Charles\Desktop\Downloads\$Filename"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)
like image 188
Bob Reynolds Avatar answered Oct 19 '22 08:10

Bob Reynolds


I got the same error as you described when I called:

$source = "http://www.website.com/file/[fileID]"
$dest = "C:\Users\Charles\Desktop\Downloads\"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $dest)

But when I changed $dest to contain the full path(including the name of the file it worked)

$dest = "C:\Users\Charles\Desktop\Downloads\[aFileName]"
like image 27
toftis Avatar answered Oct 19 '22 08:10

toftis