惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
IT之家
IT之家
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
H
Help Net Security
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 霖雨

用PowerShell命令一次启动多个前后端应用 Visual Studio Code中配置Cline 解决MSSQLLocalDB连接失败的问题 使用App Registration 访问 SharePoint Online (二) 使用App Registration 访问 SharePoint Online (一) SharePoint Online 文档库的还原功能 SharePoint Online 快捷方式功能 SharePoint Online 文档库的收藏功能 使用豆包开发SharePoint Framework项目 Power Automate 还原误删的 Flow PowerShell 获取已删除的 Automate Flow PowerShell 新建 Address Book Policy PowerShell 批量添加用户到组 PowerShell 批量下载 SharePoint Online 文档 PowerShell 新建 SharePoint Online 列表 PowerShell 获取 SharePoint Online 站点信息 PowerShell 启用 SharePoint Online CDN SharePoint Online 定时发布页面 SharePoint Online 页面诊断 Microsoft Whiteboard 的使用 SharePoint Online 网站配置时区 SharePoint Online 集成 Viva Connections SharePoint Online 分区模板 SharePoint Online 嵌入 Power BI 报表
PowerShell 清空 SharePoint Online 列表数据
霖雨 · 2026-03-01 · via 博客园 - 霖雨

  前言

  最近,在做项目的测试,然后,好多好多垃圾数据。

  正文

  1.还是先上执行成功的截图,PowerShell命令非常巴适,如下图:

image

  2.这是详细的命令,大家可以品一下,如下图:

<#
.SYNOPSIS
PowerShell 7专用 - 批量删除列表项(支持过滤条件)
.DESCRIPTION
遍历列表项并删除,可添加过滤条件,适合精准删除数据
#>

# -------------------------- 可配置变量 --------------------------
$siteUrl = "https://linyus.sharepoint.com/sites/Develop"  # 站点地址
$listName = "产品管理列表"                                      # 目标列表名称
$batchSize = 100                                                # 批量删除大小(避免请求超限)
# 可选:添加过滤条件(如只删除"是否上架"为false的项)
$filterCondition = ""  # 示例:"IsOnShelf eq false"

# -------------------------- 执行逻辑 --------------------------
# 加载PnP模块
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell)) {
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
    Install-Module -Name PnP.PowerShell -Force -AllowClobber -Scope CurrentUser
}
Import-Module PnP.PowerShell -DisableNameChecking -Force

try {
    # 连接站点
    Connect-PnPOnline -Url $siteUrl -UseWebLogin -ErrorAction Stop
    Write-Host "✅ 成功连接到站点: $siteUrl" -ForegroundColor Green

    # 检查列表是否存在
    $list = Get-PnPList -Identity $listName -ErrorAction SilentlyContinue
    if (-not $list) {
        throw "列表 '$listName' 不存在,请检查名称是否正确"
    }

    # 获取列表项(支持过滤条件)
    Write-Host "📊 正在获取列表 '$listName' 中的数据..." -ForegroundColor Cyan
    $listItems = Get-PnPListItem -List $listName -PageSize $batchSize -ErrorAction Stop #-Filter $filterCondition 
    $totalCount = $listItems.Count

    if ($totalCount -eq 0) {
        Write-Host "ℹ️ 列表 '$listName' 中无数据,无需删除" -ForegroundColor Blue
        exit 0
    }

    Write-Host "⚠️ 共获取到 $totalCount 条数据,即将批量删除" -ForegroundColor Yellow

    # 批量删除列表项
    $deletedCount = 0
    foreach ($item in $listItems) {
        Remove-PnPListItem -List $listName -Identity $item.Id -Force -Recycle:$true -ErrorAction SilentlyContinue
        $deletedCount++
        # 进度提示(每删除10条显示一次)
        if ($deletedCount % 10 -eq 0) {
            Write-Host "🔄 已删除 $deletedCount/$totalCount 条数据" -ForegroundColor Cyan
        }
    }

    Write-Host "✅ 批量删除完成!共删除 $deletedCount 条数据(数据已放入回收站)" -ForegroundColor Green
}
catch {
    Write-Host "❌ 操作失败: $($_.Exception.Message)" -ForegroundColor Red
}
finally {
    # 断开连接
    if (Get-PnPConnection) {
        Disconnect-PnPOnline -ErrorAction SilentlyContinue
    }
}

  结束语

  其实,没啥好说的,用就是了