Forum Discussion

faisalce's avatar
faisalce
Copper Contributor
Feb 13, 2023

Download file from Gdrive

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,

  • Create a scheduled task for this 

    # Define the date format for the filename
    $dateFormat = "yyyyMMdd"
    
    # Define the folder path to save the downloaded file
    $folderPath = "C:\Downloads"
    
    # Define the file name with current date
    $fileName = "ExcelFile_$(Get-Date -Format $dateFormat).xlsx"
    
    # Set up Google Drive API credentials
    $clientId = "YOUR_CLIENT_ID"
    $clientSecret = "YOUR_CLIENT_SECRET"
    $refreshToken = "YOUR_REFRESH_TOKEN"
    
    # Authenticate with Google Drive API
    $authUrl = "https://accounts.google.com/o/oauth2/token"
    $body = @{
        client_id     = $clientId
        client_secret = $clientSecret
        refresh_token = $refreshToken
        grant_type    = "refresh_token"
    }
    
    $response = Invoke-RestMethod -Uri $authUrl -Method POST -Body $body
    $accessToken = $response.access_token
    
    # Download the Excel file from Google Drive
    $fileId = "YOUR_FILE_ID"
    $url = "https://www.googleapis.com/drive/v3/files/$fileId/export?mimeType=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    $headers = @{
        Authorization = "Bearer $accessToken"
    }
    
    $response = Invoke-WebRequest -Uri $url -Headers $headers -OutFile "$folderPath\$fileName"

    faisalce 

Share

Resources