[玩转系统] 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:如何删除列表中的所有项目?
列表是 SharePoint Online 的核心组件,为用户提供灵活且可自定义的方式来存储和管理数据。但是,有时您可能需要从列表中删除项目,以清理旧数据或出于其他原因。
如何删除 SharePoint Online 中的所有列表项?要从 SharePoint Online 列表中删除所有项目,请执行以下操作:
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 中的列表?
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag