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. 以下の DeleteODBListsExportODBLis PowerShell スクリプト (またはスクリプトのカスタマイズ バージョン) を実行します。例:

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

    DeleteODBLists -siteUrl $ODBSite

このスクリプトは、これらの設定を含む非表示のリストを完全に削除します。

重要

組織内のアクティブ ユーザーの OneDrive アカウントに対してこのスクリプトを実行しないでください。

DeleteODBLists スクリプト

以下の内容をコピーし、テキスト ファイルに貼り付けます。 ファイルをDeleteODBLists.ps1として保存します。

注:

読み込まれていないアセンブリに関するエラーが発生した場合は、Add-Type パス パラメーターで定義されている SharePointPnPPowerShellOnline PowerShell モジュールの最新バージョンへのパスを再確認します。 パスは、コンピューター上で異なる場合があります。または、別のバージョンのモジュールを使用している場合があります (モジュールのバージョンはパスの一部です)。

#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"]
}