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

[玩转系统] SharePoint Online:删除过去 7 天内使用 PowerShell 创建的列表项目

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

SharePoint Online:删除过去 7 天内使用 PowerShell 创建的列表项目


要求:删除过去 7 天内在 SharePoint Online 列表中创建的所有列表项。

[玩转系统] SharePoint Online:删除过去 7 天内使用 PowerShell 创建的列表项目

PowerShell 根据创建日期删除 SharePoint Online 列表项目

您是否正在寻找一种方法来快速删除过去 7 天内创建的所有列表项? PowerShell 可以提供帮助!在本指南中,我们将向您展示如何使用 PowerShell 删除所有旧列表项:

您可以使用 PowerShell 删除过去 X 天内使用以下脚本创建的 SharePoint Online 列表中的项目。此脚本连接到 SharePoint Online 网站并使用 SharePoint 客户端对象模型 (CSOM) 检索指定列表中的项目。然后,CAML 查询用于仅检索过去 X 天内创建的项目。最后,该脚本循环遍历每个项目并将其删除,从而提供一种方便且自动化的方法来删除 SharePoint 列表数据中的旧项目。


#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"
$UserName = "[email protected]"
$Password = "Password Goes Here"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
 
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get the List
$List = $Ctx.Web.Lists.GetByTitle("Documents")

#Query to Get All List Items Created in the Past 7 Days
$CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query = "@
<View Scope='RecursiveAll'>
    <Query>        
        <Where>
                <Geq>
                    <FieldRef Name='Created' />
                         <Value Type='DateTime'><Today OffsetDays='-7'/></Value>
                </Geq>
        </Where>
    </Query>
</View>"
$CAMLQuery.ViewXml =$Query

#Get List Items matching given conditions
$ListItems = $List.GetItems($CAMLQuery)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

#Delete List Items
Write-host "Total Number of Items Matching:"$ListItems.Count
Foreach ($Item in $ListItems)
{
    $List.GetItemById($Item.id).Recycle() | Out-null
    Write-host "Deleted List Item:"$Item.Id
}
$Ctx.ExecuteQuery()

PnP PowerShell 删除过去 7 天内创建的列表项:

虽然过滤列表项的 CAML 方法是推荐的方法,但这里有另一种方法来删除过去 N 天创建的列表项。


#Variables
$SiteURL = "https://crescent.sharepoint.com"
$listName ="Documents"
$Days = -7
$UserName = "[email protected]"
$Password = "Password1"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred

#Get All List Items
$ListItems = Get-PnPListItem -List $ListName -Fields "Title","Created","ID","GUID" -PageSize 2000

#Set Cutoff Date - Past 7 days
$CutoffDate = (Get-Date).AddDays($Days)

ForEach($Item in $ListItems) 
{
    #Get the Created Date of the Item
    $CreatedDate = $Item["Created"]

    If($CreatedDate -ge $CutoffDate) 
    {
        Remove-PnPListItem -List $ListName -Identity $Item.Id -Force -Recycle
        Write-Host "Deleted List Item:"$Item.Id
    }
}

根据您的 SharePoint Online 网站、列表等替换变量的值。 $Days 变量确定从当前日期算起要删除项目的天数。在本例中,它设置为 7 天。

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

取消回复欢迎 发表评论:

关灯