Home Powershell - web requests for file downloads
Post
Cancel

Powershell - web requests for file downloads

How do I download a remote file via an API that also expects an auth Token?

1
2
3
4
5
6
7
8
9
10
11
$DownloadedFile = "C:\tmp\filename.zip";

$Url = "https://path/"
$TOKEN = "supers3crettoken"

$headers = @{
    'Accept' = 'application/octet-stream';
    Authorization = "Bearer $TOKEN";
};

Invoke-WebRequest -UseBasicParsing -Uri $Url -Headers $headers -Outfile $DownloadedFile

Alternative: use .NET WebClient

A faster way is to use the .NET WebClient instead of Invoke-Webrequest for downloading files.

I don’t remember where I read that Invoke-Webrequest copies all of the data into memory before writing it to disk which makes it very slow and resource intensive for downloading files.

There’s a corresponding TIL for downloading files with Webclient.

This post is licensed under CC BY 4.0 by the author.

Powershell - check hash of downloaded file

Git grafts - merge history of multiple repos