The $driveFileUrl variable contains a Google Drive link that starts with “https://drive.google.com/file/d/” followed by the file ID. The $localDestination points to something like “C:\downloads\myfile.zip”.
The problem is that my script runs without throwing any errors, but instead of getting the actual file, I end up with an HTML page that asks me to log into my Google account. The file I’m trying to access was shared with my account. Has anyone found a working method to download these shared files using PowerShell? I need to authenticate properly or use a different approach that bypasses the login screen.
You can fix this by changing the Google Drive URL format. Grab the file ID from your sharing URL and use this format instead: https://drive.google.com/uc?export=download&id=YOUR_FILE_ID. This skips the preview page that’s giving you the HTML login response. Works great for files under 25MB, but larger files still hit Google’s virus scan page. I’ve used this for automated backup scripts when I knew the file sizes ahead of time. Just remember - if file permissions change or the owner kills sharing access, you’ll get that same HTML auth page again.
Had the same headache a few months ago with file transfers. Your problem is Google Drive links need authentication cookies, and your basic WebClient call doesn’t have them. I switched to the Google Drive API instead of downloading from web interface URLs. You’ll need to set up a service account in Google Cloud Console and grab the credentials JSON file. Then use the Google.Apis.Drive.v3 NuGet package in PowerShell to authenticate and download files properly. More setup upfront but way more reliable than working around web authentication. Plus the API handles larger files better with resumable downloads.
Try invoke-webrequest instead of webclient - it’s better with redirects. Also check your drive link permissions are set to ‘anyone with the link’, not just specific users. I’ve hit this exact problem when the file was shared but still had restrictions.