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

[玩转系统] SharePoint Online:用于删除文档库的 PowerShell

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

SharePoint Online:用于删除文档库的 PowerShell


要求:使用 PowerShell 删除 SharePoint Online 文档库。

如何删除 SharePoint Online 中的文档库?

文档库是 SharePoint Online 的重要组成部分,非常适合存储需要经常访问的文件。但是,有时您可能想要删除不再需要的文档库。如何删除 SharePoint Online 中的文档库?嗯,这可以相对较快地完成,但重要的是要记住,存储在库中的所有文件和文件夹也将被删除。因此,请务必先备份所有重要文件!

要删除 SharePoint Online 中的文档库,请执行以下步骤:

  1. 假设您具有编辑权限:登录到 SharePoint Online 并导航到文档库。
  2. 单击“设置齿轮”>>选择“库设置”。

    [玩转系统] SharePoint Online:用于删除文档库的 PowerShell

  3. 在“权限和管理”下,单击“删除此文档库”链接。

    [玩转系统] SharePoint Online:用于删除文档库的 PowerShell

  4. 确认提示“该文档库将被删除,并且其所有文件将被删除。 您确定要将此文档库发送到网站回收站吗?”与“是”一次。

这会将文档库及其所有内容发送到回收站。这可能需要一些时间,具体取决于库的大小及其包含的项目数量。您还可以从“网站内容”页面删除 SharePoint Online 中的文档库。将鼠标悬停在文档库上,然后从上下文菜单中单击“删除”以删除文档库。

[玩转系统] SharePoint Online:用于删除文档库的 PowerShell

删除文档库是一个简单的过程,只需单击几下即可完成。好吧,让我们使用 PowerShell 删除 SharePoint Online 文档库!

SharePoint Online:用于删除文档库的 PowerShell

您可以使用 PowerShell 删除 SharePoint Online 中的文档库。首先,使用 PowerShell 连接到您的 SharePoint Online 网站。然后,从站点检索文档库。最后,使用删除或回收方法删除要删除的文档库。

以下是用于删除 SharePoint Online 中的文档库的 PowerShell:


#Load SharePoint CSOM 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"
   
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com"
$DocLibName="Docs"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
   
    #Get the Document Library
    $DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
    $Ctx.Load($DocLibrary)
    $Ctx.ExecuteQuery()

    If($DocLibrary -ne $Null)
    {
        #Delete the Document Library - Send to Recycle bin
        $DocLibrary.Recycle()

        #To Delete the Document Library - Permanently
        #$DocLibrary.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host "Document Library Deleted Successfully!" -ForegroundColor Green
    }
}
Catch {
    write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}

这将从您的 SharePoint Online 帐户中删除该库。请注意,“Recycle()”方法会删除文档库并将其发送到回收站,“DeleteObject()”方法会从 SharePoint Online 中永久删除文档库。

无法删除 SharePoint Online 中的文档库?

无法删除文档库?您是否收到“此列表无法删除。”尝试删除文档库时出现错误消息,或者在文档库设置中未获取“删除此文档库”链接?那么,要删除此类文档库,您应该先将“AllowDeletion”属性设置为“$True”,然后再删除该文档库。


#Load SharePoint CSOM 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"
   
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$DocLibName="Project Documents"

Try { 
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Set up the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $credentials
   
    #Get the Document Library
    $DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
    $Ctx.Load($DocLibrary)
    $Ctx.ExecuteQuery()

    #Set Allow Deletion Property to True to enable delete option
    $DocLibrary.AllowDeletion = $True
    $DocLibrary.Update()

    If($DocLibrary -ne $Null)
    {
        #Delete the Document Library - Send to Recycle bin
        $DocLibrary.Recycle()

        #To Delete the Document Library - Permanently
        #$DocLibrary.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host -f Green "Document Library Deleted Successfully!"
    }
}
Catch {
    write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}

PnP PowerShell 删除 SharePoint Online 文档库

使用Remove-PnPList cmdlet 删除SharePoint Online 中的文档库。以下是用于删除 SharePoint Online 中的文档库的 PnP PowerShell 脚本。请务必根据您的租户替换站点 URL 和库名称变量。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName ="Work"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive 
         
#Check if document Library exist
If(Get-PnPList -Identity $LibraryName)
{
    #powershell to remove document library
    Remove-PnPList -Identity $LibraryName -Force -recycle
    Write-host -f Green "Document Library '$LibraryName' Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Could not find Library '$LibraryName'"
}

该脚本删除文档库并将其发送到回收站!这些脚本也可用于 SharePoint 列表。只需向变量提供列表名称即可。这些 PowerShell 脚本可以使用 Visual Studio Code 或 Windows PowerShell ISE 运行。

使用 PowerShell 删除 SharePoint Online 中的所有空文档库

这次,我们需要从站点中删除所有空文档库!如果您像我一样,您可能在 SharePoint Online 中创建了太多文档库。如果您像我一样,您可能也想偶尔清理它们。 PowerShell cmdlet 来救援!


#Parameter
$SiteURL = "https://Crescent.sharepoint.com/"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
$ExcludedLibraries = @("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "Site Collection Documents","Site Collection Images",
                        "Reporting Templates","Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images")

#Get All Empty Document Libraries to Delete
$EmptyLibraries = Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $ExcludedLibraries -notcontains $_.Title -and  $_.ItemCount -eq 0}

#Delete each empty document library
ForEach($Library in $EmptyLibraries)
{
    #Delete document library in sharepoint online using powershell
    #Remove-PnPList -Identity $Library -Recycle -Force
    Write-host -f Green "Library '$($Library.Title)' Deleted Successfully!"
}

我特意对第 17 行进行了评论。只需运行该脚本一次,并确保没有其他重要文档库将被删除,然后取消注释该行(通过从中删除 #!)以删除空文档库。如果要从删除中排除任何空库,只需将其名称添加到 $ExcludedLibraries 数组中即可。

删除包含超过 5000 个文件的文档库时出现“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值”错误?那么,您必须先从 SharePoint Online 文档库中删除所有文件!

经常问的问题:

如何删除 SharePoint Online 列表?

要删除 SharePoint Online 列表,请导航到列表 >> 单击“设置”齿轮 >> 列表设置 >> 选择“删除此列表”链接。单击“确定”确认删除。如果您没有看到“删除此列表”选项,则您可能无权删除该列表。您还可以从“网站内容”页面上的上下文菜单中删除列表。
更多信息:如何在 SharePoint Online 中删除列表?

如何从 SharePoint Online 库中删除所有文件?

在浏览器中导航到 SharePoint Online 文档库 >> 单击复选标记以选择所有文件 >> 单击顶部链接栏上的“删除”按钮以删除所有文件。单击“删除”对话框上的“删除”按钮确认删除。
详细信息:PowerShell 删除 SharePoint Online 文档库中的所有文件

在 SharePoint Online 中哪里可以找到回收站?

要访问 SharePoint Online 中的回收站,请登录 SharePoint Online 网站 >> 单击设置齿轮 >> 网站内容。在网站内容页面,点击“回收站”即可进入回收站。对于第二阶段回收站,滚动到底部并单击“第二阶段回收站”链接。

如何从 SharePoint 列表中批量删除项目?

若要删除 SharePoint Online 列表中的所有列表项,请使用 PowerShell 脚本:使用 PowerShell 批量删除 SharePoint Online 列表项

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

取消回复欢迎 发表评论:

关灯