[玩转系统] SharePoint Online:使用 PowerShell 删除版本历史记录
作者:精品下载站 日期:2024-12-14 14:11:49 浏览:14 分类:玩电脑
SharePoint Online:使用 PowerShell 删除版本历史记录
SharePoint Online 中的版本控制功能允许您访问文档或列表项的先前副本。默认情况下,SharePoint Online 文档库启用版本控制。当用户进行编辑时,SharePoint 会自动创建一个包含元数据(例如创建者、时间戳等)的新版本。
如何删除 SharePoint Online 中的版本历史记录?
在这篇博文中,我们将了解如何删除 SharePoint Online 中的版本历史记录。如果您需要清除占用存储空间的旧版本文件,或者只是想清理以前的版本,这会派上用场。我们还将向您展示如何使用 PowerShell 快速轻松地删除 SharePoint Online 中的版本历史记录。
要删除 SharePoint 中文档的所有早期版本,请执行以下步骤:
SharePoint Online:使用 PowerShell 删除版本历史记录
您可以删除文档的所有版本,也可以保留最新的“N”个版本,然后删除其余版本。以下是用于删除 SharePoint Online 中的版本的 PowerShell 脚本:
情况 1:删除特定文件的所有版本:
此 PowerShell 脚本删除 SharePoint Online 中的所有版本。
#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/sites/sales/"
$FileURL="/Sites/Sales/ProjectDocuments/ProjectMetrics.xlsx"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)
#Get all versions of the file
$Versions = $File.Versions
$Ctx.Load($Versions)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Total Number of Versions Found :" $Versions.count
#Delete all versions of the file
$Versions.DeleteAll()
$Ctx.ExecuteQuery()
Write-host -f Green "All versions Deleted for given File!"
}
Catch {
write-host -f Red "Error deleting versions!" $_.Exception.Message
}
案例 2:删除库中所有文件的所有版本:
此 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/sites/Marketing"
$LibraryName="Branding"
$BatchSize = 500
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the web and Library
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
Do {
#Get items from the list in batches
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each file in the library
Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
{
#Get all versions of the file
$Ctx.Load($Item.File)
$Ctx.Load($Item.File.Versions)
$Ctx.ExecuteQuery()
Write-host "Processing Item:" $Item.file.Name
#Delete all versions of the file
If($Item.File.Versions.count -gt 0)
{
$Item.File.Versions.DeleteAll()
$Ctx.ExecuteQuery()
Write-host -f Green "`tAll Versions deleted on "$Item.file.Name
}
}
} While ($Query.ListItemCollectionPosition -ne $null)
}
Catch {
write-host -f Red "Error Deleting version History!" $_.Exception.Message
}
情况 3:保留最后五个版本并删除库中的其余版本:
这次,我们只保留最后“N”个版本,并修剪其余的版本。
#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/sites/sales/"
$LibraryName="Project Documents"
$VersionsToKeep=5
$BatchSize = 500
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the web and Library
$Web=$Ctx.Web
$List=$web.Lists.GetByTitle($LibraryName)
#Get all items from the library
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
Do {
#Get items from the list in batches
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each file in the library
Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
{
#Get all versions of the file
$Versions = $Item.File.Versions
$Ctx.Load($Versions)
$Ctx.Load($Item.File) #To get File Name
$Ctx.ExecuteQuery()
Write-host -f Yellow "Total Number of Versions Found in '$($Item.File.Name )' : $($Versions.count)"
#Check if number of versions are more than limit
While($Item.File.Versions.Count -gt $VersionsToKeep)
{
$Versions[0].DeleteObject()
$Ctx.ExecuteQuery()
Write-host -f Green "`tDeleted Version:" $Versions[0].VersionLabel
#Reload versions
$Ctx.Load($Item.File.Versions)
$Ctx.ExecuteQuery()
}
}
} While ($Query.ListItemCollectionPosition -ne $null)
}
Catch {
write-host -f Red "Error deleting versions!" $_.Exception.Message
}
修剪站点中所有库的多余版本
对于 SharePoint Online 网站的所有文档库中的所有文件,我们保留最后“N”个版本并删除其余版本:
#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"
Function Cleanup-SPOVersions
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[parameter(Mandatory=$false)][int]$VersionsToKeep = 100
)
#Config Parameters
$BatchSize = 500
#Exclude certain libraries
$ExcludedLibraries = @("Form Templates", "Preservation Hold Library", "Site Assets","Site Pages", "Images",
"Site Collection Documents", "Site Collection Images","Style Library")
#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 Libraries
$Web=$Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.Lists)
$Ctx.executeQuery()
#Get all document libraries in sharepoint online site
$DocLibraries = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $ExcludedLibraries}
#Loop through each document library and Get the Title
Foreach ($Library in $DocLibraries)
{
Write-host "Processing Document Library:"$Library.Title -f Yellow
#Get all items from the library
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged='TRUE'>$BatchSize</RowLimit></View>"
Do {
#Get items from the library in batches
$ListItems = $Library.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
#Loop through each file in the library
Foreach($Item in $ListItems | Where {$_.FileSystemObjectType -eq "File"})
{
#Get all versions of the file
$Versions = $Item.File.Versions
$Ctx.Load($Versions)
$Ctx.Load($Item.File) #To get File Name
$Ctx.ExecuteQuery()
#Calculate if versions to be cleaned for the file
$VersionsToDelete = $Versions.Count - $VersionsToKeep
If($VersionsToDelete -gt 0)
{
Write-host -f Yellow "`tTotal Number of Versions Found in '$($Item.File.ServerRelativeUrl)' : $($Versions.count)"
#Check if number of versions are more than limit
While($Item.File.Versions.Count -gt $VersionsToKeep)
{
$Versions[0].DeleteObject()
$Ctx.ExecuteQuery()
Write-host -f Green "`tDeleted Version:" $Versions[0].VersionLabel
#Reload versions
$Ctx.Load($Item.File.Versions)
$Ctx.ExecuteQuery()
}
}
}
} While ($Query.ListItemCollectionPosition -ne $null)
}
}
Catch {
write-host -f Red "Error deleting versions!" $_.Exception.Message
}
}
#Call the function to cleanup versions
Cleanup-SPOVersions -SiteURL "https://crescent.sharepoint.com/sites/marketing" -VersionsToKeep 10
如果您想保留特定文件的最后五个版本并删除其余版本,请使用:
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)
#Get all versions of the file
$Versions = $File.Versions
$Ctx.Load($Versions)
$Ctx.ExecuteQuery()
$Versionscount = $Versions.count
Write-host -f Yellow "Total Number of Versions Found :" $Versionscount
#Check if number of versions are more than limit
While($File.Versions.Count -gt $VersionsToKeep)
{
write-host "Deleting Version:" $Versions[0].VersionLabel
$Versions[0].DeleteObject()
$Ctx.ExecuteQuery()
#Reload versions
$Ctx.Load($File.Versions)
$Ctx.ExecuteQuery()
}
要按标签删除特定版本,请使用:$Versions.DeleteByLabel($LabelID)
您还可以使用 PnP PowerShell 删除 SharePoint Online 中的版本:SharePoint Online:使用 PnP PowerShell 删除版本历史记录
猜你还喜欢
- 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