删除 Microsoft OneDrive 体验设置

删除所有 Microsoft OneDrive 体验设置和信息的建议方法是:在向其他用户重新分配任何保留的文件后,删除用户的 OneDrive 网站。

管理员可以使用 PowerShell 脚本SharePoint Client-Side对象模型 (CSOM) 命令删除这些列表。 SharePointPnPPowerShellOnline Microsoft PowerShell 模块中包含所有必需的 CSOM 程序集。

可以调整本文中包含的脚本以满足需求。 例如,可以提取 的信息 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"]
}