Powershell to download file from Gdrive

Mfaisal84

New Member
Joined
Feb 13, 2023
Messages
1
Dears,
I need to download excel file from my Gdrive account with current date as filename and save it at specific folder when computer starts.

I tried from internet but could not succeed. Kindly guide me

Regards,
 

Solution
Hey try this, I haven't fully tested it, but yes it needs to be a shareable link:

Code:
# Prompt the user for the Google Drive file ID or URL
$idOrUrl = Read-Host "Enter the Google Drive file ID or URL"

# Extract the file ID from the URL if necessary
if ($idOrUrl -match "/d/([^/]+)/") {
    $fileId = $matches[1]
}
else {
    $fileId = $idOrUrl
}

# Construct the download URL
$url = "https://drive.google.com/uc?export=download&id=$fileId"

# Prompt the user for the folder path where they want to save the downloaded file
$folderPath = Read-Host "Enter the folder path where you want to save the file"

# Define the filename with the current date
$filename = "myfile_$(Get-Date -Format 'yyyy-MM-dd').xlsx"

# Combine the folder path and...

Neemobeer

Windows Forum Team
Staff member
Joined
Jul 4, 2015
Messages
8,998
Authenticated or via a shareable link? The later would be pretty simple. The former can be fairly difficult with Powershell since you'd have to handle oAuth
 

Mike

Windows Forum Admin
Staff member
Premium Supporter
Joined
Jul 22, 2005
Messages
9,250
Hey try this, I haven't fully tested it, but yes it needs to be a shareable link:

Code:
# Prompt the user for the Google Drive file ID or URL
$idOrUrl = Read-Host "Enter the Google Drive file ID or URL"

# Extract the file ID from the URL if necessary
if ($idOrUrl -match "/d/([^/]+)/") {
    $fileId = $matches[1]
}
else {
    $fileId = $idOrUrl
}

# Construct the download URL
$url = "https://drive.google.com/uc?export=download&id=$fileId"

# Prompt the user for the folder path where they want to save the downloaded file
$folderPath = Read-Host "Enter the folder path where you want to save the file"

# Define the filename with the current date
$filename = "myfile_$(Get-Date -Format 'yyyy-MM-dd').xlsx"

# Combine the folder path and filename
$filePath = Join-Path $folderPath $filename

# Download the file using the Invoke-WebRequest cmdlet
Invoke-WebRequest -Uri $url -OutFile $filePath

Its going to output everything as .xlsx so be prepared to change the script if you need to change the filename. It can only do one Gdrive URL at a home at the moment. Feel free to use this script or modify as needed.

Save as whatever.ps1 and invoke from PowerShell with ./whatever.ps1

Remember: Set-ExecutionPolicy unrestricted and then turn it back on when done (for security reasons unless you're comfortable with it off)
 

Last edited:
Solution
Back
Top