How to download files from SharePoint with MS Graph API and PowerShell

Get SharePoint Files in a site and download them to your local device

In the tutorial below I explain how you can request all files from SharePoint sites including child directories.
You can also download them right away.

There is no way in the tutorial how to do this directly with the Microsoft Graph API URLs.

I have written 2 cmdlets in a module for this myself that you can use.

Do you want to know how you can do this without a module? Then you can go to the last paragraph and consult the links of the official Microsoft Graph API docs.


Before we can start…

We need the following:


PowerShell module Optimized.Mga

I made the Optimized.Mga.SharePoint module myself. It’s a submodule for Optimized.Mga.

The module (for now) contains the cmdlets:

  • Get-MgaSharePointFiles
  • Download-MgaSharePointFiles
  • Upload-MgaSharePointFiles

    If you have feedback for me, you can leave a comment on this post, or on Github.

AzureAD registered application

Not sure how to get started with an AzureAD registered application for the Microsoft Graph API?

I wrote a page for this which you can find here:
How to start with Microsoft Graph in PowerShell by Bas Wijdenes

Microsoft also created a blog post about how to get started with an AzureAD registered application for the Microsoft Graph API.

You can find that here:
Manage app registration and API permission for Microsoft Graph notifications – Microsoft Graph | Microsoft Docs


Let’s get started with Optimized.Mga in PowerShell

Getting the Authorization token from Microsoft Graph API

Open Powershell!

To install the module & submodule you can use the following cmdlet:

Install-Module Optimized.Mga.SharePoint -Scope CurrentUser

Copy the below cmdlet.
Update the ApplicationID, ClientSecret & Tenant with the correct values.

By running the cmdlet you will create an Authorization token that other cmdlets (like Get-Mga) will automatically use in the backend.

$null = Connect-Mga -ClientSecret 'XXXX' -ApplicationID 'b5954443-ad10-4d1c-8cbc-dc05268a1858' -Tenant 'bwit.onmicrosoft.com'

If everything went well, you have received a message stating that you are logged in.


Let’s start with Get-MgaSharePointFiles

Get-MgaSharePointFiles searches for the correct site including child folders.

There are 3 parameters, of which I’ll explain the ChildFolders one abit more below.
The other two: TenantName & Site are easier to understand.

TenantName is your TenantName (With or without .onmicrosoft.com).
Site is the SharePoint Site name.

By using the following command you would retrieve all files from the root of a site:

Get-MgaSharePointFiles -TenantName 'm365x794103.onmicrosoft.com' -Site 'XXXX'

createdDateTime      : 2021-06-07T10:15:34Z
eTag                 : "{66924C60-0DDF-418D-A06A-F650A19F785E},1"
id                   : 01MIYRG5LAJSJGNXYNRVA2A2XWKCQZ66C6
lastModifiedDateTime : 2021-06-07T10:15:34Z
name                 : XXXX

There is a ChildFolders parameter that is not mandatory.
If you do not provide ChildFolders, the root is automatically used.

ChildFolders are directories in the root as in the screenshot below.

How to download files from SharePoint with MS Graph API and PowerShell
How to download files from SharePoint with MS Graph API and PowerShell

The ChildFolder parameter accepts an array of subfolders.
So for example when you want to search for files in the directory Personal as above you should use the following command:

Get-MgaSharePointFiles -TenantName 'XXXXXX.onmicrosoft.com' -Site 'XXXX' -ChildFolders 'XXXX','Personnel files','bas.wijdenes','Personal'

createdDateTime              : 2021-06-07T14:00:21Z
eTag                         : "{81017A49-4536-418C-9A71-0A03F46CD0CD},5"
id                           : 01MIYRG5KJPIAYCNSFRRAZU4IKAP2GZUGN
lastModifiedDateTime         : 2021-06-07T14:01:18Z
name                         : Document.docx

Get-MgaSharePointFiles returns all items in the directory. Including the ChildFolders.

This is because then you always see a list of all items. The disadvantage of this is that you have to filter.

When you use Get-MgaSharePointFiles and capture it in a variable $SPItems you can filter through it with Where-Object.

$SPItems = Get-MgaSharePointFiles -TenantName 'XXXXXX.onmicrosoft.com' -Site 'XXXX' -ChildFolders 'HR NL','Personnel files','bas.wijdenes','Personal'
$SPItem = $SPItems | Where-Object { $_.Name -like "*Document.docx" }

Now we have the correct file in $SPItem to start downloading.


Let’s use Download-MgaSharePointFiles to download a file from SharePoint with PowerShell

Download-MgaSharePointFiles works together with Get-MgaSharePointFiles and therefore expects an object back from Get-MgaSharePointFiles.

That is why it is useful that we have already filled the variable $SPItem in the previous chapter.

Download-MgaSharePointFiles has 2 parameters: SPItem & OutputFolder.

SPItem expects the full object $SPItem.

OutputFolder speaks for itself.
This is the path where the file will be downloaded.

To download a file from SharePoint you can use the following command:

Download-MgaSharePointFiles  -SPItem $Item -OutputFolder 'C:\temp\'

The file will always keep the same name as in SharePoint.

Do you want to download all files?
Then you can use the $SPItems in a foreach loop:

foreach ($Item in $SPItems) {
    Download-MgaSharePointFiles  -SPItem $Item -OutputFolder 'C:\temp\'
}

The official Microsoft Graph API docs

The following Microsoft Graph API docs are used in this tutorial:

Get a SharePoint site:
Get a SharePoint Site – Microsoft Graph beta | Microsoft Docs

List contents of a folder:
List the contents of a folder – Microsoft Graph v1.0 | Microsoft Docs

Download a file:
Download a file – Microsoft Graph v1.0 | Microsoft Docs

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work as a PowerShell DevOps Engineer. In my spare time I write about interesting stuff that I encounter during my work.

Leave a Reply

Your email address will not be published.