Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file with PowerShell

Tags:

powershell

I have a URL to a CSV file which, in a browser, I can download and open without issue.

I'm trying to download this file using PowerShell without success. I tried using Invoke-WebRequest, Start-BitsTransfer and using a webrequest object but no luck there.

like image 880
JustAGuy Avatar asked Jul 07 '18 17:07

JustAGuy


1 Answers

Invoke-WebRequest comes with a parameter to store its result in a file: -OutFile

Invoke-WebRequest $myDownloadUrl -OutFile c:\file.ext 

If you need authorization before you can send a request like this:

Invoke-WebRequest $myAuthUrl /* whatever is neccesary to login */ -SessionVariable MySession Invoke-WebRequest $myDownloadUrl -WebSession $MySession 

To determine the layout of the form where the login happens, you can use Invoke-WebRequests return object. It'll collect information about forms and fields on the HTML (might be Windows only). Mileage of logging in may vary with things like Two-Factor-Auth active or not. Probably you can create some secret link to your file which does not need Auth or possibly google allows you to create a private access token of some sort, which can be send aus Authorization-Header alongside your request.

like image 64
TGlatzer Avatar answered Sep 28 '22 16:09

TGlatzer