Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Remove unlicenced OneDrive sites by csv file

MS will charge you costs for unlicenced OneDrive accounts. You need to archive them in MS Syntex, buy a licence or remove it. If you would like to remove multiple SharePoint or OneDrive sites, it is possible to use a csv file. See PowerShell script below:

Write-Host "Start"
Write-Host "Connect to SP admin center"
Connect-PnPOnline "https://myTenant-admin.sharepoint.com" -Interactive -ClientId "azureAppRegistrationClientId"

$unlicencedOneDriveSitesInputCsvFilePath = "F:\scripts\unlicencedOneDriveSites.csv"
$unlicencedOneDriveSites = Import-Csv $unlicencedOneDriveSitesInputCsvFilePath | Select-Object -ExpandProperty SiteUrl
    
ForEach($unlicencedOneDriveSite in $unlicencedOneDriveSites) {
    Write-Host "Unlock OneDrive site $unlicencedOneDriveSite"
    Set-PnPSite -Identity $unlicencedOneDriveSite -LockState Unlock

    Write-Host "Remove unlicenced OneDrive site $unlicencedOneDriveSite"
    Remove-PnPTenantSite -Url $unlicencedOneDriveSite -Force
}

write-host "Finish"

Leave a Reply

Your email address will not be published. Required fields are marked *