powershellfilebackup

Delete all file and keep the latest using powershell


I have a file that i backup daily, as sample like this :

Before deleting all but the latest :

Folder C:\Backup\
File : 
Backup1-Database-A.bak | 1/10/2020 1:24 PM
Backup2-Database-A.bak | 2/10/2020 1:24 PM
Backup1-Database-B.bak | 1/10/2020 1:24 PM
Backup2-Database-B.bak | 2/10/2020 1:24 PM
Backup2-Database-B.bak | 3/10/2020 1:24 PM

Desired state after deleting all but the latest :

Folder C:\Backup\
File : 
Backup2-Database-A.bak | 2/10/2020 1:24 PM
Backup2-Database-B.bak | 3/10/2020 1:24 PM

How can I keep the latest file for each database?

I'm using this script, but it deletes all .bak so the backup of database A is gone, but I want to keep the latest for each database.

$mydays = (Get-Date).AddDays(-7)
$path = "C:\Backup\"
Get-Childitem -path $path -recurse -force | Where-Object { !$_.PSIsContainer -and $_.name -like "*.bak" -and $_.LastWriteTime -lt $mydays} | Remove-Item -Force

Solution

  • As Lee_Dailey suggests, rather than using a time window to filter by, it is simpler to sort your files by last write time and delete all but the latest files.

    The challenge is to determine which files relate to the same database and should therefore be considered as a group, so that you keep the most recent backup in each group.

    The code below uses the following approach:

    $path = 'C:\Backup'
    Get-Childitem -Path $path -Recurse -Force -File -Filter *.bak | 
      Group-Object { $_.Name -replace '^Backup\d+-' } | 
        ForEach-Object { 
          $_.Group | Sort-Object LastWriteTime -Descending | Select-Object -Skip 1 |
            Remove-Item -Force -Whatif  
        }
    

    Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

    Also note the simplification of the initial file selection: -File (PSv3+) limits output to files only (not also directories), and -Filter *.bak is an efficient way to filter by filename extension.