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

[玩转系统] SharePoint Online:使用 PowerShell 删除所有列表项

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

SharePoint Online:使用 PowerShell 删除所有列表项


要求:使用 PowerShell 从 SharePoint Online 列表中删除所有项目。

我们在 SharePoint Online - Office 365 网站中有一个列表,其中包含一堆项目,需要批量删除它们,而不是从 SharePoint Web UI 中逐个删除。因此,让我们使用 SharePoint Online PowerShell 删除所有列表项。本博客文章将向您展示如何使用 PowerShell 删除 SharePoint Online 网站中的所有列表项。

[玩转系统] SharePoint Online:使用 PowerShell 删除所有列表项

SharePoint Online:如何删除列表中的所有项目?

列表是 SharePoint Online 的核心组件,为用户提供灵活且可自定义的方式来存储和管理数据。但是,有时您可能需要从列表中删除项目,以清理旧数据或出于其他原因。

如何删除 SharePoint Online 中的所有列表项?要从 SharePoint Online 列表中删除所有项目,请执行以下操作:

  1. 浏览到您的列表,然后通过勾选列表第一列旁边的复选框来选择列表中的所有项目。这应该突出显示列表视图中的所有项目。
  2. 单击列表菜单栏中的“删除”按钮。

    [玩转系统] SharePoint Online:使用 PowerShell 删除所有列表项

  3. 确认提示。所选项目将被发送到回收站。

SharePoint 允许您一次选择 100 个项目!但是,在现代列表中,您可以通过滚动到页面底部来选择所有项目,以便 SharePoint 加载并选择其余项目。如果您的列表要大得多怎么办?嗯,PowerShell 可以节省时间!

如果没有看到复选框,请编辑列表视图 >> 在表格视图部分下,确保选中“允许单个项目复选框”!

使用 PowerShell 在 SharePoint Online 中批量删除所有列表项:

如果您正在管理 SharePoint Online,那么您很可能需要在某个时候执行批量删除。本博客文章将向您展示如何使用 PowerShell 在 SharePoint Online 列表中执行批量删除项目。以下脚本用于使用客户端对象模型 (CSOM) 和 PowerShell 删除 SharePoint Online Office 365 中的所有列表项。


#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"
  
#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

#Get Credentials to connect 
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the List items to delete
$List = $Ctx.web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()       
 
write-host "Total Number of List Items found:"$ListItems.Count
 
#SharePoint Online PowerShell to delete all list items
If($ListItems.Count -gt 0)
{
    #Loop through each item and delete
    For($i = $ListItems.Count-1; $i -ge 0; $i--)
    {
        $ListItems[$i].DeleteObject()
    } 
    $Ctx.ExecuteQuery()
         
    Write-Host "All List Items deleted Successfully!"
} 

此 PowerShell 脚本从 SharePoint Online 列表中删除所有项目。请注意,这些项目不会移至回收站,而是永久删除!从庞大列表中删除批量项目时,此 PowerShell 脚本可能非常有用。

PowerShell 从 SharePoint Online 的大型列表中删除所有项目

当您的 SharePoint 列表或库包含大量项目 (>5000) 时,您可能会遇到“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值”。错误。因此,为了缓解此问题,让我们删除批次中的项目列表。这是我的 PowerShell 脚本,用于批量删除列表项。


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"
$ListName="Projects"
$BatchSize = 500
  
#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the web and List
    $Web = $Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Write-host "Total Number of Items Found in the List:"$List.ItemCount
 
    #Define CAML Query to get list items in batches
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "<View Scope='RecursiveAll'><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
 
    Do {  
        #Get items from the list in batches
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
         
        #Exit from Loop if No items found
        If($ListItems.count -eq 0) { Break; }
 
        Write-host Deleting $($ListItems.count) Items from the List...
 
        #Loop through each item and delete
        ForEach($Item in $ListItems)
        {
            #Delete SharePoint list items
            $List.GetItemById($Item.Id).DeleteObject()
        } 
        $Ctx.ExecuteQuery()
 
    } While ($True)
 
    Write-host -f Green "All Items Deleted!"
}
Catch {
    write-host -f Red "Error Deleting List Items!" $_.Exception.Message
}

此脚本使用 PowerShell 删除 SharePoint Online 中的所有列表项。

“DeleteObject()”方法永久删除该项目。使用“Recycle()”将项目发送到回收站。

SharePoint Online PnP PowerShell 删除所有列表项

下面是用于清空 SharePoint Online 中的列表的 PnP PowerShell。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName ="Records"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Get All List Items in Batch
    $ListItems = Get-PnPListItem -List $ListName -PageSize 1000 | Sort-Object ID -Descending

    #sharepoint online powershell delete all items in a list
    ForEach ($Item in $ListItems)
    {
        Remove-PnPListItem -List $ListName -Identity $Item.Id -Force
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

使用-Recycle参数将项目发送到回收站而不是永久删除它们!

批量删除列表项的更快方法

上述方法速度较慢,删除一项大约需要 1 秒。但是,下面的脚本块方法删除项目的速度更快。


Get-PnPList -Identity $ListName | Get-PnPListItem -PageSize 100 -ScriptBlock { 
    Param($items) Invoke-PnPQuery } | ForEach-Object {$_.Recycle()
 }

使用 PnP 批处理方法更快地清除 SharePoint Online 中的列表项目

New-PnPBatch 方法相对执行批量操作更快。以下是使用 PnP Batch 方法删除所有项目的示例。


$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$ListName = "Inventory"

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

#Get List Items to Delete
$ListItems =  Get-PnPListItem -List $ListName -PageSize 500

#Create a New Batch
$Batch = New-PnPBatch

#Clear All Items in the List
ForEach($Item in $ListItems)
{    
     Remove-PnPListItem -List $ListName -Identity $Item.ID -Recycle -Batch $Batch
}

#Send Batch to the server
Invoke-PnPBatch -Batch $Batch

包起来

总之,按照本文概述的步骤,您可以使用 PowerShell 轻松删除 SharePoint Online 列表中的所有列表项。这种方法可以帮助您节省时间和精力,并确保有效管理您的数据,特别是当您有包含许多项目的大型列表时。若要从 SharePoint Online 文档库中删除所有文件,请使用:SharePoint Online:使用 PowerShell 删除文档库中的所有文件

经常问的问题:

如何使用 PowerShell 从 SharePoint Online 删除文件?

若要在 PowerShell 中从 SharePoint Online 删除文件,可以使用 SharePoint 模式和实践 (PnP) PowerShell 模块中的 Remove-PnPFile cmdlet。下面是一个从 SharePoint Online 文档库中删除文件的 PowerShell 脚本示例:Remove-PnPFile -ServerRelativeUrl “/sites/yoursite/Shared Documents/yourfile.docx”

如何使用 PowerShell 删除 OneDrive 中的文件夹?

使用 PnP Powershell 中的 Remove-PnPFolder cmdlet 删除 OneDrive for Business 中的文件夹。例如,
Connect-PnPOnline -Url“https://Crescent-my.sharepoint.com/personal/salaudeen_Crescent_com”-Interactive
Remove-PnPFolder -Name“Backup”-Folder“Documents”

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

若要删除 SharePoint Online 中的文档库,您可以使用 SharePoint Online Web 界面或 PowerShell。
要使用 Web 界面删除 SharePoint Online 中的文档库,请按照下列步骤操作: 导航到 SharePoint 网站和要删除的文档库。单击右上角的设置齿轮图标,然后从下拉菜单中选择“库设置”。单击“更多库设置”,然后单击文档库设置下的“删除此文档库”链接。您还可以使用 PowerShell cmdlet(使用Remove-PnPList)删除 SharePoint Online 中的文档库。
详细信息:如何删除 SharePoint Online 中的文档库?

如何在 PowerShell 中删除 SharePoint 列表?

要使用 PowerShell 删除列表,请使用:Remove-PnPList -Identity “List Name” -Force
更多信息:如何使用 PowerShell 删除 SharePoint Online 中的列表?

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

取消回复欢迎 发表评论:

关灯