Skip to content

Instantly share code, notes, and snippets.

@f-steff
Last active June 13, 2024 07:48
Show Gist options
  • Save f-steff/c776aa753e0c88edccee2f44ffe39047 to your computer and use it in GitHub Desktop.
Save f-steff/c776aa753e0c88edccee2f44ffe39047 to your computer and use it in GitHub Desktop.
How to set up OneDrive.exe as a Windows Service, allowing synchronization without a user is logged in.
OneDrive.exe as a Windows Service
=================================
Flemming Steffensen, 2017, 2021
For our automatic build setup, we needed to fetch some files off a Sharepoint Library.
Sharepoint allows (if configured so) Lists and Libraries to be synchronized to a local folder by using the OneDrive application.
However, the OneDrive application is started when a user logs in, and in an automated build setup, this never happens.
The solusion were to disable the normal auto-start feature of OneDrive, and then install it as a service, and making sure the
service start as the computer starts.
==========
Edit February 2021:
While this has been working flawlessly for me, others have had problems when Onedrive.exe were updated. In the comments below, @useyourblinker has made some observations and workarounds to deal with these issues. I highly recommend following his advice!
Additionally, @adrian-alephit pointed out a copy-waste issue with the Onedrive.exe Startup Directory in the text below. This has been fixed.
Huge thanks to both @useyourblinker and @adrian-alephit for their contribution!
==========
To aid this setup, the excellent NSSM tool (https://nssm.cc/) will be used.
1. Log in to the computer using the user you wish to have OneDrive running as. Ensure this user has local admin rights.
2. Install the latest Onedrive from Microsofts download page: https://www.microsoft.com/en-us/microsoft-365/onedrive/download
3. Configure OneDrive so that syncing is working.
If there are security issues, you may have to add trusted sites using IE: IE-Settings→Internet Options→ Security, select Trusted Sites and click on Sites
4. Go to OneDrive settings and ensure to remove the option for OneDrive to start as the user logs in.
Note, in the future, if you need access to the GUI, you will have to ensure the service we create is stopped, before running the OneDrive.exe as normal.
5. Open a command prompt, and change directory to the place you have installed NSSM.
6. Install the Onedrive Service by issuing the following in the command prompt: nssm.exe install "Onedrive Sync NSSM-Service"
7. In the GUI that follows, configure the tabs as follows:
Application TAB
Path: C:\Users\<USER_NAME>\AppData\Local\Microsoft\OneDrive\OneDrive.exe
Startup Directory: C:\Users\<USER_NAME>\AppData\Local\Microsoft\OneDrive
Details Tab
Display Name: OneDrive Sync
Description: Onedrive Sync Service installed using NSSM.
Startup Type: Automatic
Log On
This Account: <USER_NAME>
Password: Correct password of <USER_NAME>
Confirm: Correct password of <USER_NAME>
Dependencies: Leave as default
Process
Priority: Normal
Affinity: Select "All processor"
Console window: Select
Shutdown: Leave as default
Exit actions: Leave as default
I/O
Input (stdin):
Output (stdout): C:\OnedriveService.log
Output (stderr): C:\OnedriveService.log
File rotation
Replace existing... Not selected
Rotate files Selected
Rotate while running Selected
Restrict rotation to older 0 seconds
Restrict rotation to bigger 100000 bytes
Environment: Leave as default
End by selecting: Install
8. In Windows Services, located the new OneDrive service, and change the startup settings to run as <USER_NAME> and provide the password.
9. In Windows Services, start the new OneDrive service.
10. Verify that the synchronozation is still working.
11. Log out of the account or even better, restart the computer.
12. Use your automatic build setup to verify that the synchronization is still working.
And thats all, Folks.
@jbailey0503
Copy link

jbailey0503 commented Feb 29, 2024

I am getting this error when the service tries to open OneDrive -

Program xx\Microsoft\OneDrive\OneDrive.exe for service OneDriveSync exited with return code 3765269347.

It seems they don't want this to work anymore.

@f-steff
Copy link
Author

f-steff commented Mar 4, 2024

Thank you for the detailed troubleshooting steps, @JoelLeach - they are very valuable!

Unfortunately I'm personally unable to support this further, but I'm happy that this is still useful for a lot of people - and even more happy that people like you and others are able to support it further. Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment