Downloading Files with PowerShell

I’ve been working on PowerShell scripts lately that download executable files and run them in an RMM. The challenge is often how to download from various online sources. This article will summarize some of the key “tricks.”

The PowerShell code to download a file is pretty simple. Given parameters for the URL, local folder name, and local file name:

  $SavePath = Join-Path $SaveFolder $InstallerFileName
  $WebClient = New-Object System.Net.WebClient
  $WebClient.DownloadFile($Url,$SavePath)

The challenge is getting the correct $Url.

Web Site Host

If you have a web site and can upload files e.g. to a downloads folder, the $Url is easy:

$Url = "https://www.mysite.com/downloads/InstallFile.exe"

Google Drive

If you host the file on Google drive, be sure to share it with anyone who has the link (i.e. an unauthenticated user can download it). You can create the direct download link using the instructions in this article. However, you may still hit an intervening page telling you that anti-virus checking could not be completed (perhaps due to file size?):

PowerShell Downloads 1

Press F12 in Firefox, go to the Network tab, click the Download anyway button, and note that it ends in &confirm=t.

PowerShell Downloads 2

Once I added &confirm=t to the string, I was able to use it in a script to download without authentication. It’s not perfect—it seems slow to start sometimes (throttling?)—but it worked. So a Google Drive direct download link might look like this:

$Url = "https://drive.google.com/uc?export=download&id=[long FileID extracted from the link provided when you shared the file]&confirm=t"

Note that the Google Drive link does not reveal the filename. In order for Windows to know what to call the file, specify the local file path in the $WebClient.DownloadFile instruction as shown above.

Dropbox

If your file is hosted on Dropbox, once you have the link, the trick is to add dl=1 to the end:

$URL = "https://www.dropbox.com/s/abcdefghijklmnop123/InstallFile.exe?dl=1"

As I recall, this worked pretty well and without throttling delays.

OneDrive for Business/SharePoint

This one is a bit strange. If you share the file with anyone who has the link, then add &download=1 to the end of the URL and paste it in a browser, it starts downloading without further prompts, even in a private browser. However if you put that same string in a script, it fails consistently with 401 or 403 errors. I found several articles about how to work around this if you specify the credentials in the script, but that doesn’t help if you want the download to work anonymously. There may be a way to get it to work by specifying request headers that mimic a browser, but I gave up on this and went back to Google Drive.

SolarSendIt

www.solarsendit.net is a nice, free site for transferring files with randomized, expiring links. The URL that is generated does download the file without having to click anything further. However before starting the download, it sends a “Please wait, your download will begin shortly” screen for several seconds. That intervening stream seems to mess up the $WebClient.DownloadFile instruction—it saved a 2KB file, probably the HTML for that SolarSendIt page, rather than the 29MB executable download.

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.