Share via

PowerShell script to delete files with .xml extension older than x years on Sharepoint Online

Felipe Esteves 101 Reputation points
2021-11-19T12:41:11.417+00:00

I need a PowerShell script to delete files with .xml extension older than x years on Sharepoint Online.

This is what i have:

$SiteURL = "https://contoso.sharepoint.com/sites/Documents"

Connect-PnPOnline -Url $SiteURL

$startDate = Get-Date -Year 2016 -Month 1 -Day 1

$endDate = Get-Date -Year 2020 -Month 12 -Day 31

$ListFiles = Find-PnPFile -Folder "https://contoso.sharepoint.com/sites/Documents/Clients" -Match *.xml

$ListFiles = $ListFiles | Where-Object {($.TimeCreated.Date -le $endDate) -and ($.TimeCreated.Date -ge $startDate)}

$ListFiles.DeleteObject()

The script works, the only problem is that it can't search for files in subfolders.

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Felipe Esteves 101 Reputation points
2021-11-25T21:20:04.563+00:00

Hi.

Below PowerShell script that i used to solve my problem.

$SiteURL = "https://contoso.sharepoint.com/sites/Documents"

$FolderURL = "https://contoso.sharepoint.com/sites/Documents/Clients"

Connect-PnPOnline -Url $SiteURL

$startDate = Get-Date -Year 2016 -Month 1 -Day 1

$endDate = Get-Date -Year 2020 -Month 12 -Day 31

$ListFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File -Recursive 

$ListFiles = Find-PnPFile -Folder $FolderURL -Match *.xml

$ListFiles = $ListFiles | Where-Object {($.TimeCreated.Date -le $endDate) -and ($.TimeCreated.Date -ge $startDate)}

$ListFiles.DeleteObject()

I just added this line:

$ListFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File -Recursive

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,896 Reputation points
    2021-11-22T08:07:40.75+00:00

    Hi @Felipe Esteves ,

    Below PowerShell commands traverse all subfolders under $folderUrl, you can integrate it with your script.

    $siteURL = "https://contoso.sharepoint.com/sites/test"  
    $folderUrl = "/Shared Documents"  
         
    Function GetFolders($folderUrl)    
    {        
        $folderColl=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType Folder  
    
        foreach($folder in $folderColl)  
        {     
            $newFolderURL= $siteURL+$folderUrl+"/"+$folder.Name  
            $startDate = Get-Date -Year 2016 -Month 1 -Day 1  
            $endDate = Get-Date -Year 2020 -Month 12 -Day 31  
    
            $ListFiles = Find-PnPFile -Folder $newFolderURL -Match *.xml  
            $ListFiles = $ListFiles | Where-Object {($.TimeCreated.Date -le $endDate) -and ($.TimeCreated.Date -ge $startDate)}  
            $ListFiles.DeleteObject()  
        }         
    }  
        
    Connect-PnPOnline –Url $siteURL  
      
    # Call the functions  
    GetFolders($folderUrl)  
    

    ----------

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.