Forum Discussion

Jesper Stein's avatar
Jesper Stein
Brass Contributor
Sep 01, 2017

Removing owner from all OneDrives....

I need some help to a powershell script, that can remove an owner on all users OneDrive.

The user is added trough a 3rd party tool named ShareGate (I think by mistake) by the Sharepoint admin, so now he is owner on all OneDrive (the users are still owner too).

But I need to remove his account, and wanted to hear if any one knows a good script to do this?

    • Daniel Richardson's avatar
      Daniel Richardson
      Copper Contributor

      Had the same thing pop up in our tenant. This script helped resolve it, Thanks!!!

    • Jesper Stein's avatar
      Jesper Stein
      Brass Contributor

      Thanks for your inputs. We were lucky that Sharegate had a setting to roll back this setting so the problem is solved.

    • NCRescueTech's avatar
      NCRescueTech
      Copper Contributor
      I know this thread is old, but it is the closest I have seen to my problem.

      I may be wrong, but I think this answer only applies to the "Secondary Admin" permission. I have the problem where an outside partner is listed as an "Owner" on all personal sites/files. His name is not listed in the admin center under the "Secondary Owner". I inherited this mess, so I can't say how he got there.

      For giggles, I have tried to run the referenced script on a test site. No luck.
      • Manidurai Mohanamariappan's avatar
        Manidurai Mohanamariappan
        Iron Contributor

        NCRescueTech 

        I have rechecked the script is working fine and you need global administrator privilege to run this script. Try the below script to remove the secondary admin privilege and before run the script change the  SecondaryAdmin ,AdminURL values 

        Function Remove-OnedriveSecondaryAdmin($AdminURL,$SecondaryAdmin)
        {
        #connect Spo service.
        Connect-SPOService -Url $AdminURL
        #Get all Onedrive URL's.
        $OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
        foreach($OneDriveURL in $OneDriveURLs)
        {
        #Add Secondary administrator to Onedrive Site.
        Set-SPOUser -Site $OneDriveURL.URL -LoginName $SecondaryAdmin -IsSiteCollectionAdmin $false -ErrorAction SilentlyContinue
        Write-Host "Added secondary admin to the site $($OneDriveURL.URL)"
        }
        }
        Remove-OnedriveSecondaryAdmin -SecondaryAdmin "Admin@contoso.onmicrosoft.com" -AdminURL "https://Contoso-admin.sharepoint.com" 
  • Mario Saavedra's avatar
    Mario Saavedra
    Copper Contributor

    Hello Jesper, 
    Do you recall the steps to roll back the administrator-owner assignment to the one drives? 

    Thanks, 
    Mario

  • Kotus-Tech's avatar
    Kotus-Tech
    Iron Contributor

    Jesper SteinIt seems you've solved this but there is a setting in ShareGate under Settings > Permissions & Notifications > Auto Assign as Administrator. It looks like a handy setting to turn off but it will add the user to all OneDrives within your tenancy.

     

    After speaking to ShareGate support they sent me this link which provides a solution.

    • Edwin_Tek530's avatar
      Edwin_Tek530
      Copper Contributor

      Kotus-Tech 

       

      Prerequisites:

      • SharePoint Online PowerShell module
      • This script users ‘Set-SPOUser’ cmdlet. You must have the SharePoint Online global administrator permission to run the cmdlet.

      Add Secondary Site Collection Admin for all OD4B Users:

      Using the below Powershell script you can add the secondary site collection admin for all OD$B users.

      In the script, replace the AdminURL and SecondaryAdmin with correct values.

      Function Add-OnedriveSecondaryAdmin($AdminURL,$SecondaryAdmin)
      {
          #connect Spo service.
          Connect-SPOService -Url $AdminURL
          #Get all Onedrive URL's.
          $OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
          foreach($OneDriveURL in $OneDriveURLs)
          {
              #Add Secondary administrator to Onedrive Site.
              Set-SPOUser -Site $OneDriveURL.URL -LoginName $SecondaryAdmin -IsSiteCollectionAdmin $True -ErrorAction SilentlyContinue
              Write-Host "Added secondary admin to the site $($OneDriveURL.URL)" 
          }
      }
      Add-OnedriveSecondaryAdmin -SecondaryAdmin "email address removed for privacy reasons" -AdminURL "https://Tenantname-admin.sharepoint.com"
                          

      Remove Secondary Site Collection Admin for all OD4B Users:

      To remove the secondary site collection admin, in the above script, just change the Set-SPOUser cmdlet’s parameter “IsSiteCollectionAdmin” value to $false.

       

Resources