2

In trying to download a file with PowerShell I have the following

$client = new-object System.Net.WebClient
$client.DownloadFile($AGRIDATAMISCURL,$TESTAGRIDATAMISCZIP)

Where $AGRIDATAMISCURL is a URL that looks like "https://drive.google.com/file/d/<...>" and $TESTAGRIDATAMISCZIP looks like "C:\test\A.zip"

This script doesn't return an error but the file it downloads is basically an HTML file with a prompt to sign in to Google. Is there another way to download a file that is "shared with me"?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Kevin Burton
  • 133
  • 1
  • 1
  • 8
  • Have a look at the answer offered here: https://stackoverflow.com/questions/28985446/using-powershell-to-download-files-from-google-drive – alroc Oct 05 '16 at 17:27
  • You may also want to search for answers with the word `OAuth` and/or in combination with PowerShell or Google Drive. – Burt_Harris Oct 05 '16 at 18:10
  • The link provide although helpful mainly resolves a proxy error and a misunderstanding of file path versus folder path. Since this does not involve a proxy and a complete file path has been provided this link doesn't give any additional information. Searching for PowerShell and OAuth does provide options. But how to convert those options to Google Drive (particulary "Shared With Me" files) remains elusive. – Kevin Burton Oct 07 '16 at 12:38

3 Answers3

4

Share the file first

Files in Google Drive must be made available for sharing before they can be downloaded. There's no security context when running from PowerShell, so the file download fails. (To check this, rename the file with a `.html` extension, and view in a text editor).

Note: the following solution assumes that the links are to non-security-critical files, or that the links will only be given to those with whom access can be trusted (links are https, so are encrypted with transmission). The alternative is to programatically authenticate with Google - something not addressed in this answer.

To Share the file, in Google Drive:

  1. Right-click the file, and choose Get shareable link

    Get Shareable Link


2. Turn link sharing on

Link Sharing On


  1. Click Sharing Settings

Sharing Settings


  1. Ensure that Anyone with the link can view (Note that in corporate environments, the link must be shared with those outside the organization in order to bypass having to login)

Share with others


Then Download Programatically

  1. Then, code can be used to download the file as such (in this case with Windows PowerShell):

# Download the file
$zipFile = "https://drive.google.com/uc?export=download&id=1cwwPzYjIzzzzzzzzzzzzzzzzzzzzzzzz"
Invoke-WebRequest -Uri $zipFile -OutFile "$($env:TEMP)\myFile.doc"

  • Replace the 1cwwPzYjIzzzzzzzzzzzzzzzzzzzzzzzz with the ID code from the shareable link setting back in step #2, above.
CJBS
  • 15,147
  • 6
  • 86
  • 135
0

Here to add on @CJBS's answer. The following solution works for FILES ONLY :

Note that in 2023, Google drive share links format has changed since CJBS answer. They do not contain explicity an id anymore but rather a string of numbers and letters after an /d/ that most-likely represents the id of the shared file.

Take this share link for example :

https://drive.google.com/file/d/1SE04CUOPdUKGcloOYDpifOlamy70mcpx/view?usp=sharing

I took the link in @CJBS's answer :

"https://drive.google.com/uc?export=download&id=<INSERT_STRING_AFTER_/d/>

and just after the id= I added the 1SE04CUOPdUKGcloOYDpifOlamy70mcpx that's between /d/ and /view in my share link and my download link for Powershell became like this :

https://drive.google.com/uc?export=download&id=1SE04CUOPdUKGcloOYDpifOlamy70mcpx

My Powershell command with the download link included is the following :

Invoke-WebRequest -Uri "https://drive.google.com/uc?export=download&id=1SE04CUOPdUKGcloOYDpifOlamy70mcpx" -OutFile ./Dex.mp3
El Bachir
  • 111
  • 1
  • 4
0

A confirm parameter is now needed in order to download the file without confirmation. I'm using the following approach with WebClient:

$WebClient = New-Object System.Net.WebClient
$Url = "https://drive.google.com/uc?export=download&id=YOUR_ID_HERE&confirm=t"
$WebClient.DownloadFile($Url, "C:\Download\file_name.zip")
GaboBrandX
  • 2,380
  • 14
  • 25