共用方式為


刪除 Microsoft OneDrive 體驗設定

刪除所有 Microsoft OneDrive 體驗設定及資訊的建議方式,就是在將任何保留的檔案重新指派給其他使用者之後,移除使用者的 OneDrive 網站。

系統管理員可以使用 PowerShell 腳本SharePoint Client-Side 物件模型 (CSOM) 命令來刪除這些清單。 所有必要的 CSOM 元件都包含在 SharePointPnPPowerShellOnline Microsoft PowerShell 模組中。

您可以調整本文中包含的腳本,以符合您的需求。 例如,您可以擷取 的資訊 user1@contoso.com ,如下所示:

  1. 為自己指派使用者的 OneDrive 帳戶權限。 可在 Microsoft 365 系統管理中心完成這項動作,方法如下所示。

  2. 安裝所需的 Microsoft PowerShell 模組:

    Install-Module SharePointPnPPowerShellOnline

    Install-Module CredentialManager

  3. 請執行以下的 DeleteODBLists PowerShell 指令碼 (或指令碼的自訂版本),例如:

    $ODBSite = "https://contoso-my.sharepoint.com/personal/user1_contoso_com"

    DeleteODBLists -siteUrl $ODBSite

此指令碼會永久刪除包含這些設定的隱藏清單。

重要事項

對於組織中仍在作用中的使用者,請勿在 OneDrive 帳戶上執行此指令碼。

DeleteODBLists 指令碼

複製下列內容,並將它們貼到文字檔中。 將檔案儲存為DeleteODBLists.ps1。

注意事項

如果您看到有關元件未載入的錯誤,請仔細檢查最新版 SharePointPnPPowerShellOnline PowerShell 模組的路徑,如 Add-Type Path 參數中所定義。 您的電腦上的路徑可能不同,或者您可能使用不同版本的模組, (模組版本是路徑) 的一部分。

#DeleteODBLists
#Deletes OneDrive experience settings, stored in several SharePoint Lists
param([string]$siteUrl, [bool]$useStoredCreds=$true)
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.26.1805.0\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.26.1805.0\Microsoft.SharePoint.Client.Runtime.dll"

if (!$siteUrl)
{
    Write-Host "Please specify a OneDrive site using -siteUrl."
    return
}

if ($useStoredCreds)
{
    Write-Host "Retrieving stored Windows credentials for $siteUrl."
    $cred = Get-StoredCredential -Target $siteUrl
    if (!$cred)
    {
        Write-Host "Didn't find stored credential for $siteUrl. Please provide credentials to connect."
        $cred = Get-Credential
    }
}
else
{
   $cred = Get-Credential
}

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName,$cred.Password)
$webURL = $siteUrl
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
$ctx.Credentials = $credentials

#Root folders of lists to export
$SWMRoot = "Reference " #starts with this string
$notificationsRoot = "notificationSubscriptionHiddenList6D1E55DA25644A22"
$activityFeedRoot = "userActivityFeedHiddenListF4387007BE61432F8BDB85E6"
$accessRequestsRoot = "Access Requests"
$microfeedRoot = "PublishedFeed"
$SPHomeCacheRoot = "SharePointHomeCacheList"
$sharingLinksRoot = "Sharing Links"
$socialRoot = "Social"


#Get all lists in the web
try{
    $lists = $ctx.web.Lists
    $ctx.load($lists)
    $ctx.executeQuery()
}
catch{
    write-host "$($_.Exception.Message)" -foregroundcolor red
}


#Process all lists and identify the settings lists to be deleted
$listsToDelete = @()
foreach($list in $lists)
{
    $ctx.load($list)
    $ctx.load($list.RootFolder)
    $ctx.executeQuery()
    $listTitle = [string]$list.Title
    $listRoot = $list.RootFolder.Name
    $listTemplateId = $list.TemplateFeatureId

    Write-host ("Processing List: " + $list.Title + " with " + $list.ItemCount + " items").ToUpper() -ForegroundColor Yellow
    Write-host (">> List Root Folder: " + $listRoot) -ForegroundColor Yellow

    if ($listRoot.StartsWith($SWMRoot,"CurrentCultureIgnoreCase") -and $list.ItemCount -ge 1)
    {
        Write-Host ">> Found: Shared With Me List" -ForegroundColor Green
        $listDetails = @{listType = "Shared With Me List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $notificationsRoot)
    {
        Write-Host ">> Found: Notifications List" -ForegroundColor Green
        $listDetails = @{listType = "Notifications List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $activityFeedRoot)
    {
        Write-Host ">> Found: User Activity Feed List" -ForegroundColor Green
        $listDetails = @{listType = "User Activity Feed List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $accessRequestsRoot)
    {
        Write-Host ">> Found: Access Requests List" -ForegroundColor Green
        $listDetails = @{listType = "Access Requests List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $microfeedRoot)
    {
        Write-Host ">> Found: MicroFeed List" -ForegroundColor Green
        $listDetails = @{listType = "Microfeed List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $SPHomeCacheRoot)
    {
        Write-Host ">> Found: SharePoint Home Cache List" -ForegroundColor Green
        $listDetails = @{listType = "SharePoint Home Cache List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $sharingLinksRoot)
    {
        Write-Host ">> Found: Sharing Links List" -ForegroundColor Green
        $listDetails = @{listType = "Sharing Links List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
    elseif ($listRoot -eq $socialRoot)
    {
        Write-Host ">> Found: Social List" -ForegroundColor Green
        $listDetails = @{listType = "Social List"; listTitle = $listTitle; listRoot = $listRoot; listTemplateId = $listTemplateId}
        $listsToDelete += $listDetails
    }
}

#Retrieve web features
$webFeatures = $ctx.Web.Features
$ctx.Load($webFeatures)
$ctx.ExecuteQuery()

#Export list function
function deleteList
{
    Param ([string] $listTitle, [string] $listTemplateId)

    Write-Host ("Deleting List: " + $listTitle).ToUpper() -ForegroundColor Red

    #Remove features the list may depend on
    $webfeatures.Remove($listTemplateId, $true)
    $ctx.executeQuery()

    #Set the list to allow deletion
    $list = $lists.GetByTitle($listTitle)
    $list.AllowDeletion = $true
    $list.Update()
    $ctx.executeQuery()

    #Delete the list
    $list.DeleteObject()
    $ctx.executeQuery()
}

#Delete all target lists
foreach ($list in $listsToDelete)
{
    deleteList -listTitle $list["listTitle"] -listTemplateId $list["listTemplateId"]
}