使用 PnP PowerShell 从 SharePoint 列表中删除所有项目
最近,我收到一个紧急要求,要求删除 SharePoint Online 列表中的所有项目。 PowerShell 是批量删除列表项的最佳选择。在本教程中,我将向您展示如何使用 PnP PowerShell 和 PowerShell 删除 SharePoint 列表中的所有项目。
我建议使用 PnP PowerShell 从 SharePoint 列表中删除项目。
在所有示例中,我将使用下面的 SharePoint Online 列表。该列表有几个项目,如下面的屏幕截图所示。
使用 PnP PowerShell 从 SharePoint Online 列表中删除所有项目
PnP PowerShell 提供了使用 SharePoint 列表的有效方法。
以下是使用 PnP PowerShell 从 SharePoint 列表中删除所有项目的完整脚本。
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"
# Get all items in the list
$items = Get-PnPListItem -List $listName
# Loop through and delete each item
foreach ($item in $items) {
Remove-PnPListItem -List $listName -Identity $item.Id -Force
}我使用 VS code 执行了上述脚本,您可以在下面的屏幕截图中看到所有 SharePoint 列表项已被删除。
删除 SharePoint 列表项时,您还可以定义页面大小。这是一个完整的脚本。
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"
# Get all items in the list
$items = Get-PnPListItem -List $listName -PageSize 2000
# Loop through and delete each item
foreach ($item in $items) {
Remove-PnPListItem -List $listName -Identity $item.Id -Force
}
# Disconnect the session
Disconnect-PnPOnline读取使用 PnP PowerShell 将数据从 Excel 导入到 SharePoint 列表
使用 PnP PowerShell 批量删除 SharePoint 大列表项目
如果您要从大型 SharePoint 列表中删除所有项目,则该过程可能会有点慢。批量删除可以显着加快该过程。使用PnP PowerShell,可以轻松批量删除项目。
这是完整的代码。
Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/MarketingTeam" -Interactive
$listName = "ProjectTasks"
# Get all items in the list
$items = Get-PnPListItem -List $listName
# Create a batch
$batch = New-PnPBatch
# Queue deletion of each item
foreach ($item in $items) {
Remove-PnPListItem -List $listName -Identity $item.Id -Batch $batch -Force
}
# Execute the batch
Invoke-PnPBatch -Batch $batch这里 :
- 带有
-Interactive参数的Connect-PnPOnlinecmdlet 将提示您以交互方式登录。 Get-PnPListItem检索列表项,-PageSize 2000帮助处理更大的列表。Remove-PnPListItem用于删除每个项目。
这是如何使用 PnP PowerShell 批量删除大型 SharePoint Online 列表中的项目。
阅读使用 PnP PowerShell 将 5000 多个项目添加到 SharePoint Online 列表
使用 PowerShell 从 SharePoint 列表中删除所有项目
在第一种方法中,我将向您展示如何使用 PowerShell (SharePoint Online Management Shell) 从 SharePoint Online 列表中删除所有项目。如果您不熟悉此操作,可以先下载并安装 SharePoint Online Management Shell。
以下是用于从 SharePoint 列表中删除所有项目的完整 PowerShell 脚本。
$siteUrl = "https://szg52.sharepoint.com/sites/MarketingTeam"
$listName = "ProjectTasks"
# Connect to the site
Connect-SPOService -Url $siteUrl
# Get the list
$list = Get-SPOList -Identity $listName
# Get all items in the list
$items = Get-SPOListItem -List $list
# Loop through and delete each item
foreach ($item in $items) {
Remove-SPOListItem -List $list -Identity $item.Id -Confirm:$false
}执行上述脚本后,SharePoint 列表中的所有项目都将被删除。
在本教程中,我解释了如何使用 PnP PowerShell 从 SharePoint Online 列表中删除所有项目以及如何使用 PnP PowerShell 从大型 SharePoint 列表中批量删除所有项目。
如果您仍有疑问,请随时在下面发表评论。

