Upload a File to OneDrive via Graph API and PowerShell

Recently, I upgraded my Invoicing automation, so I create Invoices automatically, upload them to OneDrive, and send them via Mail.

Of course, for this, I used GRAPH API to send the PDF to our OneDrive. In my Automation, I got the bits from our Invoicing Service and sent them to OneDrive without downloading a File.

In this Post today, we show a simple example, and the advanced one will follow.

So first, we need to create an App Registration with proper Permissions

API Reference and Permissions

Read the following DOCS for more Details

https://learn.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0

Create an Azure App Reg with the following GRAPH API Application Permissions

  • Files.ReadWrite.All
  • Files.Read.All

All done, then let’s see the Script

The Steps

First, let’s explain the Workflow.

The full script with authentication is below, and on my GitHub Repo, I want to explain some steps for better understanding here.

In this example, we have some local Files, Word, PDF, Text, and a PNG. We want to upload those files to my Onedrive in the Foilder “0-Temp,” which is in the Root Directory of my Onedrive.

First, we need to get the Drive.

$Drive = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drive" -Method GET -Headers $headers_OneDrive

Next, we need our destination Folder “0-Temp.” If you want to get a Folder in a Subfolder, just enter the whole Path.

$DestFolder = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/root:/0-Temp" -Method GET -Headers $headers_OneDrive 

Now we can build the Final URL to upload a File

$File="Hello World.docx"

$fileName=$File.Split("\")[-1]

#Upload
Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'application/docx'

Full Script

$clientID_OneDrive = "your Client ID"
$Clientsecret_OneDrive = "your Secret"
$tenantID = "your Tenant ID"

$GraphBaseURL="https://graph.microsoft.com/v1.0"

#OneDrive User
$UserUPN="michael.seidl@au2mator.com"

#Authentication
$tokenBody_OneDrive = @{
    Grant_Type    = "client_credentials"
    Scope         = "https://graph.microsoft.com/.default"
    Client_Id     = $clientID_OneDrive
    Client_Secret = $Clientsecret_OneDrive
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody_OneDrive
$headers_OneDrive = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

#Get the Drive ID
$Drive = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drive" -Method GET -Headers $headers_OneDrive

#Get the Folder
$DestFolder = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/root:/0-Temp" -Method GET -Headers $headers_OneDrive 





#Word Document
$File="Hello World.docx"
$fileName=$File.Split("\")[-1]
#Upload
Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'application/docx'


#pdf Document
$File="Hello World.pdf"
$fileName=$File.Split("\")[-1]
#Upload
Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'application/pdf'


#TXT Document
$File="Hello World.txt"
$fileName=$File.Split("\")[-1]
#Upload
Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'plain/text'



#Image
$File="Hello World.png"
$fileName=$File.Split("\")[-1]
#Upload
Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'image/png'

GitHub Repo

As I constantly update my Code and Examples, please always get my Script from GitHub. They might be newer and with fewer Bugs πŸ™‚

Make sure you get the Script from my Azure Github Repo: Seidlm/Microsoft-Graph-API-Examples (github.com)

Name: Upload File to Onedrive.ps1

Michael Seidl, aka Techguy,
au2mate everything
#Automationmindset

Leave a Comment

Your email address will not be published. Required fields are marked *

*