Updated: Use PowerShell Module OneDrive from PowerShellGallery to work with OneDrive via command line

Marcel Meurer's picture

 wrote a PowerShell module to work with OneDrive via command line. The module use the documented OneDrive web API from Microsoft (https://dev.onedrive.com/README.htm). If you already have the module PowerShellGet to access PowerShellGallery installed, you can easily download this module with a single command:

Install-Module -Name OneDrive -Scope CurrentUser

You can update the module to a newer version with the same command (add -force). If you dont use PowerShellGet currently, go to the Gallery on https://www.powershellgallery.com/packages/OneDrive and click on “Get Started”.

If you have any questions or feature requests, write me an email or get in touch with me via www.twitter.com/MarcelMeurer

Features (version 0.9.2 or higher)

The actual version of this module supports:

  • Authentication with OneDrive (half web-based)
  • Get properties of folder and files
  • List folders and files
  • Search files and folders
  • Create folders
  • Delete folders and files
  • Download files
  • Upload files (up to 100 Mbyte)

Next features to be implemented:

  • Rename files and folders
  • Check if an unattended login for authentication without a web form is possible
  • ...   

How to use the module (examples)

The functions accept a wider range of parameters as used in these examples. To display all parameters, examples and a description use “get-help”:

get-help Get-ODChildItems -detailed

To display all functions of this module type:

Get-Command -Module OneDrive

You can address files and folders through their path (-path) or element id (-ElementID).


For each interaction with the OneDrive API you need an access token. You can generate a fresh token for reading and writing access (this lasts for 1 hour) with:

$Authentication=Get-ODAuthentication -ClientID "00000000…….."


Get-ODAuthentication opens a browser window to authenticate yourself with your live id and to accept access through this module. 

You can get your own client id on https://apps.dev.microsoft.com

Update: Make sure that you generate a client id in the section 'Converged applications' for the token-based authentication and select "Allow implicit flow". Here is a short video: https://youtu.be/oyILyPQ066E

List all OneDrives available to your account (normally only one)

Get-ODDrives -AccessToken $AuthToken

id               driveType owner    quota

--               --------- -----    -----

8badcff017eaa324 personal  @{user=} @{deleted=21419952; remaining=22945965909; state=normal; total=42949672960; used...


List folders and files in your OneDrive root:

Get-ODChildItems -AccessToken $AuthToken -Path "/" | ft

name                 size lastModifiedDateTime     id                     folder

----                 ---- --------------------     --                     ------

Backups        1377598479 2015-10-30T19:24:08.84Z  8BADCFF017EAA324!4036  @{childCount=2}

Bilder            1503567 2016-02-19T20:28:03.243Z 8BADCFF017EAA324!12091 @{childCount=2}

Data                    0 2016-02-21T12:17:54.26Z  8BADCFF017EAA324!12158 @{childCount=4}

Daten               24606 2015-12-07T09:03:17.877Z 8BADCFF017EAA324!4034  @{childCount=1}

Dokumente       968612599 2016-02-19T10:25:43.923Z 8BADCFF017EAA324!129   @{childCount=4}

Eigene Bilder 14963858114 2016-02-19T20:29:37.057Z 8BADCFF017EAA324!1205  @{childCount=42}

Musik                   0 2015-10-01T12:00:24.467Z 8BADCFF017EAA324!11786 @{childCount=0}

Videos         2689233875 2015-11-04T11:33:21.09Z  8BADCFF017EAA324!9602  @{childCount=1}

demo.xls          2873344 2016-02-19T20:57:18.08Z  8BADCFF017EAA324!12156

Notes.txt            2467 2016-02-15T20:01:15.1Z   8BADCFF017EAA324!12102

List folders and files in the folder “/data”

Get-ODChildItems -AccessToken $AuthToken -Path "/data" | ft

name      size lastModifiedDateTime     id                     folder

----      ---- --------------------     --                     ------

backup       0 2016-02-21T12:17:11.64Z  8BADCFF017EAA324!12160 @{childCount=0}

documents    0 2016-02-21T12:17:54.26Z  8BADCFF017EAA324!12161 @{childCount=2}

log files    0 2016-02-21T12:17:02.043Z 8BADCFF017EAA324!12159 @{childCount=0}

pictures     0 2016-02-21T12:17:36.97Z  8BADCFF017EAA324!12162 @{childCount=0}

List folders and files in the folder “data” by element id:

Get-ODChildItems -AccessToken $AuthToken –ElementID 8BADCFF017EAA324!12158

(same as above)

Create a new folder “2016” under “/data/documents”

New-ODFolder -AccessToken $AuthToken -Path "/data/documents" -FolderName "2016"

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives('me')/items('root%252Fdata%252Fdocuments')/childr


createdBy            : @{application=; user=}

createdDateTime      : 2016-02-21T12:36:22.337Z

cTag                 : adDo4QkFEQ0ZGMDE3RUFBMzI0ITEyMTY1LjYzNTkxNjU0OTgyMzM3MDAwMA

eTag                 : aOEJBRENGRjAxN0VBQTMyNCExMjE2NS4w

id                   : 8BADCFF017EAA324!12165

lastModifiedBy       : @{application=; user=}

lastModifiedDateTime : 2016-02-21T12:36:22.337Z

name                 : 2016

parentReference      : @{driveId=8badcff017eaa324; id=8BADCFF017EAA324!12161; path=/drive/root:/Data/documents}

size                 : 0

webUrl               : https://onedrive.live.com/redir?resid=8BADCFF017EAA324!12165

fileSystemInfo       : @{createdDateTime=2016-02-21T12:36:22.337Z; lastModifiedDateTime=2016-02-21T12:36:22.337Z}

folder               : @{childCount=0}

Upload a file to OneDrive “/data/documents/2016”

Add-ODItem -AccessToken $AuthToken -Path "/Data/documents/2016" -LocalFile "AzureML with PowerShell.docx"

StatusCode        : 201

StatusDescription : Created

Content           : {"@odata.context":"https://api.onedrive.com/v1.0/$metadata#drives('me')/items/$entity","@content.do



RawContent        : HTTP/1.1 201 Created

                    Content-Location: https://mczd5q.dm2302.livefilestore.com/y3p5-QWu8f3PAjkT2zaXz-j4h_viQ6QeiYcItUYJb


Forms             : {}

Headers           : {[Content-Location, https://mczd5q.dm2302.livefilestore.com/y3p5-QWu8f3PAjkT2zaXz-j4h_viQ6QeiYcItUY


                    mahWs7DF7GrMEtOV3DPTg_3WwQFU3k601mW_3YfokTBZBmTcyAqe7p9D6], [X-WLSPROXY, BN1301____PAP100],

                    [X-MSNSERVER, DM2302____PAP236], [Strict-Transport-Security, max-age=31536000;


Images            : {}

InputFields       : {}

Links             : {}

ParsedHtml        : mshtml.HTMLDocumentClass

RawContentLength  : 1435

Get some metadata of a file or folder

Get-ODItemProperty -AccessToken $AuthToken -Path "/Data/documents/2016/AzureML with PowerShell.docx"

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives('me')/items(name,size,lastModifiedDateTime,id,fol


name                 : AzureML with PowerShell.docx

size                 : 110804

lastModifiedDateTime : 2016-02-21T13:07:41.11Z

id                   : 8BADCFF017EAA324!12169

Get all metadata of a file or folder by element id ("" select all properties)
Get-ODItemProperty -AccessToken $AuthToken -ElementID 8BADCFF017EAA324!12169 -SelectProperties ""

@odata.context       : https://api.onedrive.com/v1.0/$metadata#drives('me')/items/$entity

@content.downloadUrl : https://public.dm2302.livefilestore.com/y3m3fRaVdsz2KgyxKlgGlK6CO744YSaXyk0llE9uhvg96DFAgjdjkjBL



createdBy            : @{application=; user=}

createdDateTime      : 2016-02-21T13:07:41.11Z

cTag                 : aYzo4QkFEQ0ZGMDE3RUFBMzI0ITEyMTY5LjI1Nw

eTag                 : aOEJBRENGRjAxN0VBQTMyNCExMjE2OS4z

id                   : 8BADCFF017EAA324!12169

lastModifiedBy       : @{application=; user=}

lastModifiedDateTime : 2016-02-21T13:09:32.287Z

name                 : AzureML with PowerShell.docx

parentReference      : @{driveId=8badcff017eaa324; id=8BADCFF017EAA324!12165; path=/drive/root:/Data/documents/2016}

size                 : 110804

webUrl               : https://onedrive.live.com/redir?resid=8BADCFF017EAA324!12169

file                 : @{hashes=; mimeType=application/vnd.ms-word.document.12}

fileSystemInfo       : @{createdDateTime=2016-02-21T13:07:41.11Z; lastModifiedDateTime=2016-02-21T13:07:41.11Z}

Download a file from OneDrive

Get-ODItem -AccessToken $AuthToken -Path "/Data/documents/2016/Powershell array custom objects.docx"


If “0” returnes, the download was successful

Delete an item

Remove-ODItem -AccessToken $AuthToken -Path "/Data/documents/2016/Azure-big-picture.old.docx"


If “0” returnes, the download was successful

Search for items in a sub folder recursively

Take a look at OneDrive API documentation to see how the search (preview) works (file and folder names, in file search, …)

Search-ODItems -AccessToken $AuthToken -Path "/My pictures" -SearchText "FolderA" | ft

name       size lastModifiedDateTime     id                     folder

----       ---- --------------------     --                     ------

FolderA       0 2016-02-18T15:03:28.637Z 8BADCFF017EAA324!12106 @{childCount=0}

FolderA 10    0 2016-02-18T15:05:28.94Z  8BADCFF017EAA324!12116 @{childCount=0}

FolderA 1     0 2016-02-18T15:03:43.337Z 8BADCFF017EAA324!12107 @{childCount=0}

FolderA 3     0 2016-02-18T15:03:50.28Z  8BADCFF017EAA324!12109 @{childCount=0}

FolderA 8     0 2016-02-18T15:05:02.11Z  8BADCFF017EAA324!12114 @{childCount=0}

FolderA 7     0 2016-02-18T15:04:58.11Z  8BADCFF017EAA324!12113 @{childCount=0}

FolderA 4     0 2016-02-18T15:04:40.04Z  8BADCFF017EAA324!12110 @{childCount=0}

FolderA 13    0 2016-02-18T15:05:36.23Z  8BADCFF017EAA324!12119 @{childCount=0}

FolderA 12    0 2016-02-18T15:05:33.76Z  8BADCFF017EAA324!12118 @{childCount=0}

FolderA 14    0 2016-02-18T15:05:45.253Z 8BADCFF017EAA324!12120 @{childCount=0}

FolderA 2     0 2016-02-18T15:03:45.927Z 8BADCFF017EAA324!12108 @{childCount=0}

FolderA 9     0 2016-02-18T15:05:09.72Z  8BADCFF017EAA324!12115 @{childCount=0}

FolderA 6     0 2016-02-18T15:04:46.03Z  8BADCFF017EAA324!12112 @{childCount=0}

FolderA 11    0 2016-02-18T15:05:31.397Z 8BADCFF017EAA324!12117 @{childCount=0}

FolderA 5     0 2016-02-18T15:04:43.11Z  8BADCFF017EAA324!12111 @{childCount=0}


Delete all files and folders found with Search-ODItems (be careful)

foreach ($element in Search-ODItems -AccessToken $AuthToken -Path "/My pictures" -SearchText "FolderA") {Remove-ODItem -AccessToken $AuthToken -ElementID $element.id}
















Download all files found with Search-ODItems

foreach ($element in Search-ODItems -AccessToken $AuthToken -Path "/My pictures" -SearchText "Marcel") {Remove-ODItem -AccessToken $AuthToken -ElementID $element.id}





Available functions

CommandType     Name                                               Version    Source

-----------     ----                                               -------    ------

(internal)        Format-ODPathorIDString                            0.9.2      OneDrive

Function        Get-ODAuthentication                               0.9.2      OneDrive

Function        Get-ODChildItems                                   0.9.2      OneDrive

Function        Get-ODDrives                                       0.9.2      OneDrive

Function        Get-ODItem                                         0.9.2      OneDrive

Function        Get-ODItemProperty                                 0.9.2      OneDrive

(internal)        Get-ODWebContent                                   0.9.2      OneDrive

Function        New-ODFolder                                       0.9.2      OneDrive

Function        Add-ODItem                                         0.9.2      OneDrive

Function        Remove-ODItem                                      0.9.2      OneDrive

Function        Search-ODItems                                     0.9.2      OneDrive




Hi, sehr cool von dir die Mühe. Allerdings scheint es mir hemmend, wenn man sich per Browser authentifizieren muss. Wenn das auch direkt in PS ginge, würde ich mir das Ganze genauer ansehen und definitiv Verwendung dafür haben. Anyway, feine Sache.
How do I get my user id that I need in $Authentication=Get-ODAuthentication -ClientID "00000000…….." regards
Marcel Meurer's picture
Client ID
You can get your own client id on https://dev.onedrive.com/app-registration.htm#register-your-app-for-onedrive
Client ID? or Application ID?
At that URL I can configure a new Application ID - but I can't see a way of getting a Client ID. Might be that the application registration process has changed since you posted this. Any chance you can check it?
Marcel Meurer's picture
Client ID? or Application ID?
Hi Richard. Makes sure that you generate a 'Live SDK application' instead of a 'Converged application'. The id should be like this '000000004C1xxxxx' and not like a Guid. Marcel
Ein klein wenig Refactoring
Hallo, ich habe angefangen ein klein wenig refactoing an deinem Modul vorzunehmen. Im Moment experimentiere ich auch an einer Lösung was then AUTH ohne Browser angeht. https://github.com/jhochwald/PoSH-PSOneDrive Es steht dir natürlich frei, meine Änderungen zu verwenden, oder eben auch nicht ;-)
Marcel Meurer's picture
Ein klein wenig Refactoring
Hallo Jörg. Vielen Dank und würde ich gerne übernehmen. Ich habe auch noch die Anmeldung unter Umgehung auf dem Plan und würde es dann aktualisieren. Viele Grüße und Danke Marcel
Ich erhalte den folgende Fehler wenn ich versuche das Script auszuführen: PS L:\_tools\OnedriveSync> $ResponseObject=Get-ODWebContent -AccessToken $AuthToken -Method GET -rURI "/drives" Get-ODWebContent : Cannot access the api. Webrequest return code is: At line:1 char:17 + ... ponseObject=Get-ODWebContent -AccessToken $AuthToken -Method GET -rUR ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-ODWebContent irgend welche ideen woran das liegen könnte? die Autentifizierung konnte erfolgreich abgeschlossen werden
Marcel Meurer's picture
Hallo Gerd. Du verwendest die interne Funktion Get-ODWebContent. Wenn du z.B. Inhalte deines Standard-Drives ausgeben möchtest, würde ich folgenden Befehl verwenden: Get-ODChildItems -AccessToken $AuthToken -Path "/" Falls du doch Get-ODWebContent nutzen möchtest, erfordert Pfad eine bestimmte Syntax, z.B. für den Inhalt des Ordners 'Daten': Get-ODWebContent -AccessToken $AuthToken -Method GET -rURI "/drive/root:/Daten:/children" Viele Grüße Marcel
Hallo Marcel,
Hallo Marcel, ich bekomme leider auch bei Get-ODWebContent die gleiche Antwort: Kann dir gerne mein script schicken - es geht darum alle Bilder und videos von einem Folder in OneDrive lokal herunterzulanden. Viele Grüße Gerd
Marcel Meurer's picture
Hallo Gerd.
Hallo Gerd. Sehr gerne. Schicke es doch an marcel.meurer@sepago.de.
Hello there, how do I get the value of @content.downloadUrl. Thanks in advance.
Marcel Meurer's picture
Hi. Try this with -SelectProperties: Get-ODItemProperty -AccessToken $aut.access_token -Path "/demo.xls" -SelectProperties ""
Transfer files from OneDrive to Local Server
Hi, I'm not sure if it is possible to transfer files from OneDriver to Local Server.
Marcel Meurer's picture
Hi. Try this to download the excel file to an unc-path: Get-ODItem -AccessToken $aut.access_token -Path "/demo.xls" -LocalPath \\server\share\folder
Hello, thanks for your reply and I've done it but got another question. Is it possible to do automatic authentication without clicking "yes"? (I have to schedule and run this script every day for example.)
Marcel Meurer's picture
Hi. Take a look here: https://www.sepago.com/blog/2016/08/01/unattended-login-for-onedrive-via-script-feasibility-test It's a bit freaky ;-)
Auto Authentication
Thank you very much. :)
Firefox Web Browser
Hello, thank you for your post. But do you have solution for Firefox Web Browser? Thanks in advance.
The property 'Value' cannot be found on this object
I was using Windows 7 x64 but didn't remember exactly which IE version that I used but got the error after reinstall OS as follows. The property 'Value' cannot be found on this object. Verify that the property exists and can be set. At C:\dev\OneDrive\scripts\oneDriveLogin.ps1:45 char:1 + $ie.Document.IHTMLDocument3_getElementsByName("login").item().Value=$ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
How to get a list all users in OneDrive in Powershell
Is there a script to use to generate a list of all user in onedrive (and their folders) using powershell. Please help!
Get list of users, folders, in Active Directory in Onedrive
How generate a list of all users in Active directory with Onedrive using Powershell
OneDriver Path
Hello there, First of all, sorry that I've asked too many questions so far. How do I get the OneDrive path?
Marcel Meurer's picture
No problem. But what do you
No problem. But what do you want to do? The OneDrive path for the root in your OneDrive is "/"
OneDrive path
Thank you. For example, I know the name of the file on OneDrive and I would like to know path of that the file below to.
Marcel Meurer's picture
OneDrive path
Hi. Try this to seach for a file named '_notes.txt': Search-ODItems -AccessToken $at -SearchText "_notes.txt" -SelectProperties "name,size,parentReference"
How to get the OneDrive sync staus?
Is this possible to get the Sync status i.e. completed/incompleted/in progress thru powershell function?
Get-ODItemProperty and images tags
Hi , Thanks a lot for your job. Clear & usefull ! I cannot retrieve personel tags set on a image like 'mydogname'. I see them on one drive and on Windows when I dl the file but not by Get-ODItemProperty. Only this one are displayed : http://puu.sh/t2p3R/cb9aa62d21.png I use the ' -SelectProperties "" ' option. Did I miss something ? Thx
Marcel Meurer's picture
Get-ODItemProperty and images tags
Hi kazed. Unfortunately, the tags now are not accessible through the api. The is an uservoice for adding this to the api: https://onedrive.uservoice.com/forums/262982-onedrive/suggestions/7866669-include-tags-in-api "*" is correct to get all properties. Sorry Marcel
Thanks for your answer.
Thanks for your answer.
Restoring deleted onedrive Business files from recycle bin
Hi, I'm looking for an powershell script to restore onedrive business deleted files from recycle bin.
Restoring deleted onedrive Business files from recycle bin
Hi, I'm looking for an powershell script that would help to restore the deleted files on a specified date from onedrive Business
Hi, Is there a command for removing a file from 'Shared with me' using powershell?
One drive link
Hello Marcel, i have OneDrive installed on my PC. In Windows Explorer I can right click on a OneDrive file, then click 'Share a OneDrive link' to obtain the OneDrive file URL for my selected file. Is it possible to obtain this file link programmatically using your solution?
Marcel Meurer's picture
One drive link
Hello Nigel. Unfortunatly not now. But I will check the api for this. Marcel
Shared Embedded URL
Does the API (via powershell) allow me to create an embedded, shared URL for a jpg file?
How to read data from One Drive ExcelSurvey to Powershell
Hi Marcel, How to read data from One Drive ExcelSurvey to Powershell? I am using SharePoint 2013 and Powershell 3.0. and OneDrive with same account credentials.
Can't get the token
Hi Marcel, very helpful tutorial but for some reason when i want to generate a token using the : $Authentication=Get-ODAuthentication -ClientID "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" $AuthToken=$Authentication.access_token All i get is a window popping and the next second it disappears so i can't authorize anything, is this method still actual ?
Marcel Meurer's picture
Re: Can't get the token
Hi Victor. I tried it out and it still works. Are you logged in with an Microsoft account in your IE session? If so, try to log out bevore. Please give me a feedback. Marcel
Same Issue: Resolved
Hi Victor, I had the same issue recently. I had forgotten to click the "Add Platform" --> "Web" --> check "Allow Implicit Flow" in the application setup. After I saved the app the Microsoft Login window did not disappear. Hopefully that may help. -JM
Thanks for sharing this. It was very helpful in cleaning up a bunch of duplicate files which OneDrive created for some reason.
Is this an error in your examples page?
Hi, the last example which says it downloads items uses Remove-ODItem, isn't this for deleting or have I misunderstood? I didn't want someone trying this out and deleting a bunch of stuff by mistake: Download all files found with Search-ODItems foreach ($element in Search-ODItems -AccessToken $AuthToken -Path "/My pictures" -SearchText "Marcel") {Remove-ODItem -AccessToken $AuthToken -ElementID $element.id}
issues with access_token and livesdk application for onedrive
Hi , when try to run the first part of the script $Authentication=Get-ODAuthentication -ClientID "myclientID" I get a white popup that I can't modify after a couple of seconds it dissapears. then I get the warning that I get this warning WARNING: There is maybe an errror, because there is no access_token! furthermore How do I get the possibility to get the different Id's that you have under the live SDK applications thanks for your help Paul
Marcel Meurer's picture
Aw: issues with access_token and livesdk application for onedriv
Hi Paul. I updated the blog post (see above). Important is that you create a converged app and allow implicit flow. Take a look here (I made a short video): https://youtu.be/oyILyPQ066E
Live SDK applications missing
Hi Marcel, Things seemed to have changed slightly over at: https://apps.dev.microsoft.com, there is no distinction anymore between Converged applications and Live SDK applications. I've created an application but the Client id is not working for me when I try to generate the token, probably because as you mention it's a guid-styled application id. How to resolve this?
Marcel Meurer's picture
Aw: Live SDK applications missing
Hi Phil. I updated the blog post (see above). Important is that you create a converged app and allow implicit flow. Take a look here (I made a short video): https://youtu.be/oyILyPQ066E
AccessToken longer than 1 hour
Hi, Is it possible to configure the Access length of time to something greater than 1 hour. I'm trying to upload a very larger number of files using this module but it only last for an hour and the token expires.
Marcel Meurer's picture
Hi Dipla.
Hi Dipla. It's not possible. But take a look here: https://www.sepago.com/node/2994 With the new version you are able to refresh the token. Marcel
This is extremely useful, thanks! Does it work for any of the below functions? If not, are you considering adding features for them? I wasn't able to use it to: * Manage remoteItems (i.e. shared files and folders) * Upload large files * Upload non local files using a url
Marcel Meurer's picture
AW: Features
Hi Aaron. Sorry. This features are not implemented yet.
Add new comment
By submitting this form, you accept the Mollom privacy policy.