web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / uploading file on goog...
Power Automate
Answered

uploading file on google drive using api - Power shell script PAD

(0) ShareShare
ReportReport
Posted on by 357

Hi Team,

 

I am trying to upload file on google drive using api Powershell script.

file get added on google drive but file name is not comming its come Untitled,

 

also not able to view the upladed file 

 

what is an issue 

Here is my script

# Web API endpoint URL
$apiUrl = %Google%

# Input file path
$inputFilePath = "C:\Users\sanket.shinde\Downloads\Krishna.png"


# API token
$apiToken = "provided"

$fileName = "Demo"

try {
    # Create a WebClient object
    $webClient = New-Object System.Net.WebClient

    # Set the Authorization header with the API token
    $webClient.Headers.Add("Authorization", "Bearer $apiToken")

    # Upload the file using multipart/form-data
    $responseBytes = $webClient.UploadFile($apiUrl, "POST", $inputFilePath)

    Write-Output "file saved successfully."
}
catch {
    Write-Output "An error occurred: $_"
}

I have the same question (0)
  • Verified answer
    kinuasa Profile Picture
    801 Most Valuable Professional on at

    The code in the link below may be a useful reference for how to upload files using the Google Drive API in PowerShell.

     

    *GDrive Upload PowerShell Script - GitHub

    https://gist.github.com/ConnorGriffin/dc804357bb10ff7522d0e21ddfdf9398

     

    Alternatively, if you are using the API Client Library for .NET, the code might look something like the one below.

     

    Add-Type -AssemblyName "System.Web"
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Newtonsoft.Json.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Core.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Auth.dll")
    [void][Reflection.Assembly]::LoadFile("C:\System\Lib\Google.Apis.Drive.v3.dll")
    
    $clientId = "(Client ID)"
    $clientSecret = "(Client Secret)"
    $credentialPath = "C:\wk\GoogleAPI\client_secrets.json"
    $uploadFilePath = "C:\Test\SamplePDF.pdf"
    #[String[]]$parents = @("1*_**********_*****************") #Folder ID
    [String[]]$parents = @("root")
    
    #Get AccessToken
    $clientSecrets = [Google.Apis.Auth.OAuth2.ClientSecrets]::new()
    $clientSecrets.ClientId = $clientId
    $clientSecrets.ClientSecret = $clientSecret
    [String[]]$scopes = @("https://www.googleapis.com/auth/drive")
    $credential = [Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker]::AuthorizeAsync(
     $clientSecrets, 
     $scopes, 
     "user", 
     [System.Threading.CancellationToken]::None, 
     [Google.Apis.Util.Store.FileDataStore]::new($credentialPath, $true)).Result
    
    #Upload file to Google Drive
    $service = [Google.Apis.Drive.v3.DriveService]::new()
    $uploadFileName = [System.IO.Path]::GetFileName($uploadFilePath)
    $mimeType = [System.Web.MimeMapping]::GetMimeMapping($uploadFilePath)
    $stream = [System.IO.FileStream]::new($uploadFilePath, [System.IO.FileMode]::Open)
    $body = [Google.Apis.Drive.v3.Data.File]::new()
    $body.Name = $uploadFileName
    $body.MimeType = $mimeType
    $body.Parents = $parents
    $request = $service.Files.Create($body, $stream, $mimeType)
    $request.AccessToken = $credential.Token.AccessToken
    $request.Upload()
    $stream.Close()

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 247 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 164 Super User 2026 Season 2

#3
trice602 Profile Picture

trice602 138 Super User 2026 Season 2

Last 30 days Overall leaderboard