Simple PowerShell script to compress SQL Bacup files

1–2 minutes

Originally from

Found, then improved from

Found, then improved

Repost

the wolf squeezing a pile of big database backup files into small boxes with a press, one amber box, the raven shelving

PowerShell
# Handle BAK and TRN files
get-childitem -recurse |
where { $_.extension -match ".(bak|trn)" -and -not (test-path ($_.fullname -replace "(bak|trn)", "7z")) } |
foreach { &7za a -t7z -mx=9 ($_.fullname -replace "bak", "7z") $_.fullname }

# Delete any BAK or TRN that has a matching 7z file
get-childitem -recurse |
where { $_.extension -match ".(bak|trn)" -and
(test-path ($_.fullname -replace "(bak|trn)", "7z")) } |
foreach { del $_.fullname }