Home > OS >  Can I upload a file to onedrive via Windows 10 command line?
Can I upload a file to onedrive via Windows 10 command line?

Time:05-25

I need to upload a file to OneDrive, via the command line. This will be done through a batch file which is distributed to end users.

From searching on Stack Overflow, I find questions like this one which say that you need to register an app and create an app password, using Azure. I don't have the necessary permissions to do this in the organization where I work, nor can I do anything that requires an admin account. So I can't any install software - I have to use what comes with Windows 10. I can't use VBA either as that's blocked.

I've managed to download files from OneDrive without anything like that, using the process described here:

  • Open the URL in either of the browser.
  • Open Developer options using Ctrl Shift I.
  • Go to Network tab.
  • Now click on download. Saving file isn’t required. We only need the network activity while browser requests the file from the server.
  • A new entry will appear which would look like “download.aspx?…”.
  • Right click on that and Copy → Copy as cURL.
  • Paste the copied content directly in the terminal and append ‘--output file.extension’ to save the content in file.extension since terminal isn’t capable of showing binary data.

Example:

curl https://xyz.sharepoint.com/personal/someting/_layouts/15/download.aspx?UniqueId=cefb6082-696e-4f23-8c7a%2

…. some long text …. cCtHR3NuTy82bWFtN1JBRXNlV2ZmekZOdWp3cFRsNTdJdjE2c2syZmxQamhGWnMwdkFBeXZlNWx2UkxDTkJic2hycGNGazVSTnJGUnY1Y1d0WjF5SDJMWHBqTjRmcUNUUWJxVnZYb1JjRG1WbEtjK0VIVWx2clBDQWNyZldid1R3PT08L1NQPg==; cucg=1’ --compressed --output file.extension

I tried to do something similar after clicking 'upload' on the browser, but didn't find anything useful when trying to filter the requests.

I found these two questions but there is no keyboard shortcut to upload, AFAICT. Also the end user will be uploading a file to a folder I've shared with them from my OneDrive. Opening Chrome or Edge as a minimised window is fine, but I can't just shove a window in their face which automatically clicks on things - they won't like that.

It's just occurred to me that I might be able to use an office application to Save As the file to the necessary onedrive folder, where the keyboard shortcuts are pretty stable, but have no idea how to achieve that via the command line.

CodePudding user response:

The best and more secure way to accomplish this goal I think is going to be with the Rest API for OneDrive.

(Small Files <4MB) https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content?view=odsp-graph-online

(Large files) https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online

You still need a Azure AD App Registration (which your admin should be able to configure for you), to provide API access to services in Azure. Coding with the API is going to be far easier and less complicated, not to mention more versatile.

  • Related