Automatically sync team site document libraries using PowerShell and Intune

Have you tried deploying the “configure team site libraries to sync automatically” policy in Intune, and ran into various conflicts/errors?

Solution – Just use PowerShell.

First, we need to enable OneDrive Files On Demand, and the option to silently sign in users to the OneDrive sync app on each device that will use this configuration.

This could be done either via the script, or using an Intune configuration profile which is what I did in my test (but do keep in mind a script would be quicker to apply):

  • Silently sign in users to the OneDrive sync app with their Windows credentials
  • Use OneDrive Files On-Demand

Next, you will create a version of the script below for each site you need to mount, making sure to change “Site1” (which needs to be unique for each version site1, site2, etc.) and add the Library ID.

$MountRegKey="HKLM:\SOFTWARE\Policies\Microsoft\OneDrive\TenantAutoMount"
$autoMountLibraries= @{
    #Replace Site1 with a unique name for each site that will be mounted, and add the Library ID
    Site1="Library ID Here"
}

if (-not (Test-Path $MountRegKey)){
    
    New-Item -Path $MountRegKey -Force

}

$autoMountLibraries.GetEnumerator() | ForEach-Object {
        
    Set-ItemProperty -Path $MountRegKey -Name $PSItem.Key -Value $PSItem.Value -ErrorAction Stop
}

The Library ID can be retrieved either from the Sharepoint site directly by clicking “Sync” > Copy Library ID or if it does not show, then using this method: Get Library ID from SharePoint Document Library for Sync · GitHub (win2000b)

Deploy the script(s) either using Intune’s built-in script function, or as a Win32 App:

If deploying via Intune Script, choose the following settings:

  • run this script using the logged on credentials No
  • enforce script signature check No
  • run 64 bit powershell. Yes

Note: According to Microsoft peeps on the following podcast, the sync function will be retired in the near future in favor of shortcuts: ‎The Intrazone by Microsoft: Add to OneDrive op Apple Podcasts