The script below can be used to remove secondary admins from or add secondary admins to, Onedrive for Business sites.
Ideally, you need to be either a global admin or SharePoint admin to run this, and you will need the SharePoint PowerShell module installed.
This will run for ALL Onedrive sites in your organisation.
###Script written by jimmy white.
Clear-Host
###Add or remove? add = $true, remove = $false
$addorremove = $false
###Change Tenant name below
Connect-SPOService -url "https://YOURTENANT-admin.sharepoint.com/"
#change the person you want to remove, comma sepearted and in quotes:
$secadmins = @("
us***@YO*******.ORG
","
us***@YO*******.ORG
")
#Dont change anything below this line!
$start = Get-Date
[nullable[double]]$secondsRemaining = $null
$users = Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Url -like '-my.sharepoint.com/personal/'" | select Url
$progress = 1
foreach ($user in $users){
$percentComplete = [math]::Round((100/$users.count) * $progress,2)
$progressParameters = @{
Activity = "Working on sites [$($progress)/$($users.Count)] $($secondsElapsed.ToString('hh\:mm\:ss'))"
Status = 'Processing'
CurrentOperation = "Removing permissions from " + $user.url
PercentComplete = $percentComplete
}
if ($secondsRemaining) {
$progressParameters.SecondsRemaining = $secondsRemaining
}
Write-Progress @progressParameters
$site= $user.'Url'
foreach($secadmin in $secadmins){
Set-SPOUser -Site $site -LoginName $secadmin -IsSiteCollectionAdmin $addorremove
write-host "OneDrive Admin Access for $secadmin removed from $site"
}
$progress++
$secondsElapsed = (Get-Date) – $start
$secondsRemaining = ($secondsElapsed.TotalSeconds / $progress) * ($users.Count – $progress)
}