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

[玩转系统] SharePoint Online:使用 PowerShell 删除文档库中超过 30 天的所有文件

作者:精品下载站 日期:2024-12-14 21:18:20 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 删除文档库中超过 30 天的所有文件


要求:删除 SharePoint Online 文档库中超过 30 天的所有文件。

[玩转系统] SharePoint Online:使用 PowerShell 删除文档库中超过 30 天的所有文件

PowerShell 可删除 SharePoint Online 中超过 30 天的文件

我必须从 SharePoint Online 文档库中删除所有超过 30 天的文件。此 PowerShell 允许您从 SharePoint Online 文档库中删除 30 天前(或更长时间!)创建的所有文件。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/ops/"
$LibraryName = "Documents"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Define Query to Filter Files that were 'Created' 30 days ago (or More!)
$Query= "<View Scope='RecursiveAll'>
            <Query>
                <Where>
                    <And>
                        <Lt>
                            <FieldRef Name='Created' Type='DateTime'/>
                            <Value Type='DateTime' IncludeTimeValue='TRUE'>
                                <Today OffsetDays='-30'/>
                            </Value>
                        </Lt>
                        <Eq>
                            <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                        </Eq>
                    </And>
                </Where>
            </Query>
        </View>"

#Get All Files matching the query 
$Files = Get-PnPListItem -List $LibraryName -Query $Query -PageSize 500
  
#Loop through each File
Write-host -f Green "Total Number of Files Found:"$Files.Count
ForEach($File in $Files)
{ 
    #Send File to recycle bin
    Write-Host "Deleting File Created On:" $File.FieldValues.Created -f Yellow
    Move-PnPListItemToRecycleBin -List $LibraryName -Identity $File.Id -Force
    Write-Host "`tDeleted File at:" $File.FieldValues.FileRef -f Green
}

同样,您也可以根据“修改”日期删除文件!

删除 SharePoint Online 网站上超过 1 年的所有文件

删除过去 1 年或更长时间未更新的所有文件怎么样?那么,下面的脚本会扫描给定站点中所有文档库中的所有文件,并根据上次修改日期生成超过 1 年或更久的文件的报告。只需运行此脚本一次,浏览它生成的 CSV 文件。一旦您有信心,您可以取消注释第 36 行以实际删除文件。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVPath = "C:\Temp\FilesDeleted.csv"
$DataCollection = @()

#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
   
#Get all Documents Libraries from the site
$ExcludedLists  = @("Style Library", "Wiki", "Form Templates","Images","Pages","Site Pages","Preservation Hold Library","Site Assets")
$DocumentLibraries = Get-PnPList | Where {$_.Hidden -eq $False -and $_.ItemCount -gt 0 -and $ExcludedLists -notcontains $_.Title -and $_.BaseType -eq "DocumentLibrary"}

#Loop through all document libraries
ForEach ($List in $DocumentLibraries)
{
    #Get all Files that are not modified in the past 1 year or more!
    $global:counter = 0;
    $ListItems = Get-PnPListItem -List $List -PageSize 2000 -Fields Created, Modified -ScriptBlock `
        { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity `
             "Getting Documents from Library '$($List.Title)'" -Status "Getting Files $global:Counter of $($List.ItemCount)";} | ` 
                 Where {$_.FileSystemObjectType -eq "File" -and $_.FieldValues.Modified -lt (Get-Date).AddDays(-365)}

    $ItemCounter = 0
    #Iterate through each item
    Foreach ($Item in $ListItems)
    {
        $DataCollection +=New-Object PSObject -Property ([Ordered] @{
            Name  = $Item.FieldValues.FileLeafRef
            RelativeURL = $Item.FieldValues.FileRef
            CreatedOn = $Item.FieldValues.Created
            ModifiedBy =  $Item.FieldValues.Editor.Email
            ModifiedOn = $Item.FieldValues.Modified
        })

        #Delete the File
        #Remove-PnPListItem -List $List -Identity $Item.ID -Recycle -Force

        $ItemCounter++
        Write-Progress -PercentComplete ($ItemCounter / ($ListItems.Count) * 100) -Activity "Deleting Files from Library '$($List.Title)' $ItemCounter of $($ListItems.Count)" -Status "Deleting file '$($Item['FileLeafRef'])"        
    }
}
#Export data to CSV File
$DataCollection | Export-Csv -Path $CSVPath -NoTypeInformation

使用 PnP PowerShell 删除超过 30 天的文件

要删除 SharePoint Online 中早于特定日期的文件,我们也可以使用 PnP PowerShell。让我们删除创建时间超过 30 天的所有文件。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$LibraryName = "Documents"
$NumberOfDaysToKeep = 30
$FileType = "*.log"

#Connect to site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Files Older than 30 days
$OlderFiles = Find-PnPFile -Match $FileType -List $LibraryName | Where-Object { $_.TimeCreated -lt $((Get-Date).AddDays(-$NumberOfDaysToKeep)) }
Write-host "Total Number of Files Found:"$OlderFiles.count

#Delete all older files
If($OlderFiles.count -gt 0)
{
    $OlderFiles | ForEach-Object { 
        Remove-PnPListitem -identity ($_.Listitemallfields) -Recycle -Force | Out-Null
        Write-host "Removed File:"$_.ServerRelativeUrl
    }
}

请注意,此脚本将删除文件并将其发送到回收站。如果您想永久删除它们而不移至回收站,请保留“-Recycle”开关。

概括

总之,本指南向您展示了如何在 SharePoint Online 中使用 PowerShell 删除早于特定日期的文件。通过遵循提供的示例脚本,您可以轻松地自动执行清理旧文件或过时文件的过程,从而节省时间和精力。但请注意,此脚本将删除文件,并将它们移动到回收站,因此在运行此脚本之前,请决定是要永久删除它们还是将它们发送到回收站。另外,请确保您具有适当的权限,并且在运行脚本之前,您的计算机上已安装 SharePoint Online PowerShell 模块和 PnP PowerShell 模块。

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

取消回复欢迎 发表评论:

关灯