Disable OneDrive Sync for SharePoint Online Site Libraries


https://gallery.technet.microsoft.com/Disable-OneDrive-Sync-for-71f99ceb

Write-Host “Please Enter SharePoint Online Service Administrator Credentials!”
$Creds = Get-Credential

$SuperAdmin = “SCAadmin@tenant.onmicrosoft.com”

$AdminUrl = ‘https://admin.sharepoint.com’

Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll”

Connect-SPOService -Url $AdminUrl -Credential $Creds
$spocredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName , $Creds.Password)

$Sites = Get-SPOSite -Limit All | Select-Object Url
Write-Host “Total Sites are ” $Sites.Count -ForegroundColor Green

foreach($Site in $Sites)
{
    try
    {
        Set-SPOUser -Site $Site.Url -LoginName $SuperAdmin -IsSiteCollectionAdmin $true -Verbose
        $SiteUrl = $Site.Url
        Write-Host $SiteUrl

        #Get the Client Context and Bind the Site Collection
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
        $ctx.Credentials = $spocredentials
        #Authenticate
        $Rootweb = $ctx.Site.RootWeb
        $ctx.Load($Rootweb)
        $ctx.Load($Rootweb.Webs)
        $ctx.ExecuteQuery()

        Write-Host $Rootweb.Title
        write-host $Rootweb.ExcludeFromOfflineClient

        $Rootweb.ExcludeFromOfflineClient = $true
        $Rootweb.Update()
        $ctx.ExecuteQuery()

        #Fetch the users in Site Collection
        foreach($web in $Rootweb.Webs)
        {
            try
            {
                $ctx.Load($Web)
                $ctx.ExecuteQuery()
                Write-Host $web.Title
                write-host $web.ExcludeFromOfflineClient
                $web.ExcludeFromOfflineClient = $true
                $web.Update()
                $ctx.ExecuteQuery()
            }
            catch
            {
                Write-Host “Exception at Sub Web. Script will continue” -ForegroundColor Red
            }
        }

    }
    catch
    {
        Write-Host “Exception at Root Web. Script will continue” -ForegroundColor Red
    }
}