当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 清空回收站

作者:精品下载站 日期:2024-12-14 14:16:47 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 清空回收站


要求: 使用 PowerShell 清空 SharePoint Online 中的回收站。

如何清空 SharePoint Online 中的回收站?

SharePoint Online 中的回收站功能用于保留已删除的项目,例如列表项、文档、列表和文档库,类似于台式电脑上的 Windows 回收站。回收站中已删除的项目会占用 93 天保留期的存储空间并消耗网站集配额。因此,我们应该立即清除回收站以重新获得空间。作为 SharePoint Online 管理员,您可以清空回收站以释放存储空间。您可以清空整个 SharePoint Online 环境或单个 SharePoint 网站集的回收站。这可以通过 Web 浏览器或使用 PowerShell 来完成。

要在网站级别清除 SharePoint Online 回收站,请执行以下步骤:

  1. 导航到您的 SharePoint Online 站点,然后单击站点设置齿轮 >> 选择“站点内容”。
  2. 在“网站内容”页面中,单击右上角的“回收站”链接。 (在现代团队站点和通信站点中,您可以单击导航菜单栏中的“回收站”来访问回收站。)

    [玩转系统] SharePoint Online:使用 PowerShell 清空回收站

  3. 您现在可以单击工具栏中的“清空回收站”按钮!或者,您可以选中所有项目复选框,然后单击“删除选择”链接,从 SharePoint Online 的回收站中删除项目。这会将项目移动到第二阶段回收站。
  4. 清空网站集回收站 SharePoint Online:如果您拥有网站集管理员权限,则可以使用“网站集回收站”页面,位置为“设置”>>“网站信息”>>“查看所有网站设置”>>“回收站”(在“网站集管理”下)可访问网站集中所有用户和所有网站的已删除项目(URL 快捷方式:/_layouts/15/AdminRecycleBin.aspx?view=5)。
  5. 清空 SharePoint Online 中的第二阶段回收站:清除第一阶段回收站后,您可以单击“第二阶段回收站”链接来访问第二阶段回收站(打开第一阶段回收站)阶段回收站,然后使用第二阶段回收站的快捷方式:/_layouts/15/AdminRecycleBin.aspx?view=5#view=13)并清空它。

    [玩转系统] SharePoint Online:使用 PowerShell 清空回收站

顺便说一句,您至少应该具有编辑权限才能访问和恢复回收站中已删除的内容,并且第二阶段或网站集回收站中的已删除项目将被永久删除。

SharePoint Online:使用 PowerShell 清空回收站

因此,我们手动清空了 SharePoint Online 回收站。但如果有一种方法可以自动化呢? PowerShell 来救援!我将向您展示如何使用 PowerShell 清空 SharePoint Online 回收站。以下是用于清空 SharePoint Online 中回收站的 PowerShell:


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $UserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    Write-Host "Total Number of Items found Recycle Bin:" $RecycleBinItems.Count
    #sharepoint online powershell empty recycle bin
    $RecycleBinItems.DeleteAll()
    $Context.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

这将永久删除网站集第一阶段和第二阶段回收站中的所有项目。

SharePoint Online:清空第一阶段回收站

有时,您可能必须从任一回收站中删除项目。如果您想清空第一阶段或第二阶段回收站,请使用这些脚本:

PowerShell从回收站删除


Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Move All Deleted Items to 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.MoveAllToSecondStage()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PowerShell 清空 SharePoint Online 中的第二阶段回收站

如何删除第二阶段回收站中的文件?那么,要清空网站集的第二阶段回收站,请使用以下 PowerShell 脚本:


Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    
    #Delete All Items from 2nd Stage Recycle bin
    $Ctx.Site.RecycleBin.DeleteAllSecondStageItems()
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

这会清空 SharePoint Online 中的网站集回收站。

SharePoint Online:PowerShell 从回收站删除网站

要从回收站永久删除网站集,请使用:


$Cred = Get-credential
Connect-SPOService -url "https://crescent-admin.sharepoint.com/" -credential $Cred
Get-SPODeletedSite | Select URL 

要永久删除已删除的 SharePoint Online 网站集:


Remove-SPODeletedSite -identity $DeletedSiteCollURL -Confirm:$False

SharePoint Online:使用 PnP PowerShell 清空回收站

要清除第一阶段和第二阶段回收站,请使用以下 PowerShell 脚本:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Empty Reycle Bins: Both 1st stage and 2nd Stage
Clear-PnPRecycleBinItem -All -Force

如何清除回收站中包含 5000 多个已删除项目的项目?那么,为了避免超出阈值的问题,我们必须批量清空已删除的项目。具体方法如下:


#Config Variables
$SiteURL =  "https://crescent.sharepoint.com/sites/marketing"

#Connect to Site collection
Connect-PnPOnline -Url $SiteURL -Interactive

#Get recycle bin items in batches and delete them permanently
While( (Get-PnPRecycleBinItem -RowLimit 500) -ne $null)
{
    Get-PnPRecycleBinItem -RowLimit 500 | Clear-PnPRecycleBinItem -Force
}

要清空第二阶段回收站,请使用 -SecondStageOnly 开关。您还可以使用:Move-PnpRecycleBinItem cmdlet 清除第一阶段回收站。这是针对 SharePoint 本地部署的另一篇文章:PowerShell 清空 SharePoint 中的回收站

从 SharePoint 回收站清除旧项目

SharePoint 回收站会将已删除的项目保留 93 天。如果您想清除所有旧项目(例如 30 多天前删除的项目)怎么办?


#Parameters
$SiteURL =  "https://crescent.sharepoint.com/sites/Retail"
$Threshold = 30 #Number of days

#Calculate Deleted Date threshold
$DeletedDateThreshold = (Get-Date).AddDays(-$Threshold)

#Connect to the Site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get recycle bin items deleted more than specific number of days
$DeletedItems = Get-PnPRecycleBinItem -RowLimit 500000 | Where {$_.DeletedDate -le $DeletedDateThreshold}

#Remove deleted items permanently
$DeletedItems | Clear-PnPRecycleBinItem -Force

同样,要清除租户回收站并永久删除已删除的网站,请使用:如何清空 SharePoint Online 中的租户回收站?

经常问的问题:

如何在 SharePoint Online 中恢复已删除的文件?

要恢复 SharePoint Online 中已删除的文件: 登录 SharePoint 网站 >> 单击“设置”齿轮,选择“网站内容”>> 单击右上角的“回收站”按钮。选择您要恢复的文件,然后单击“恢复”按钮。这会将项目恢复到其原始位置。如果找不到已删除的项目,请尝试第二阶段回收站。您还可以使用 PowerShell 恢复 SharePoint Online 中的文件。
详细信息:在 SharePoint Online 中恢复已删除的文件

如何使用 PowerShell 删除 SharePoint Online 列表中的所有项目?

浏览到您的 SharePoint 列表,然后通过勾选列表第一列旁边的复选框来选择列表中的所有项目。单击“删除”按钮并确认提示。所选项目将被发送到回收站。 SharePoint 允许您一次选择 100 个项目!但是,您可以通过滚动到页面底部来选择所有项目。您还可以使用 PowerShell 删除 SharePoint Online 中的所有列表项。
详细信息:SharePoint Online 从列表中删除所有项目

如何删除 SharePoint Online 网站集?

您可以从 SharePoint 管理中心、网站的网站设置或使用 PowerShell 删除 SharePoint Online 网站。要从 SharePoint Online 网站集删除,请按照下列步骤操作: 以网站集管理员身份登录 SharePoint >> 单击“设置”齿轮 >> 网站信息 >> 单击“删除网站”链接!
更多信息:删除 SharePoint Online 中的网站集

如何访问 SharePoint Online 中的第二阶段回收站?

要访问 SharePoint Online 中的第二阶段回收站,请以网站集管理员身份登录 SharePoint 网站 >> 单击“设置”齿轮图标 >> 选择“网站内容”。在“网站内容”页面上,单击右上角的“回收站”链接。向下滚动到底部,然后单击“第二阶段”回收站链接。第一阶段回收站到第二阶段的URL快捷方式:/_layouts/15/AdminRecycleBin.aspx?view=5#view=13

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯