PowerShell – Deleting those files

Usually I prefer to use C# to delete files (as part of my retention policy for backups) as a stored procedure (SQLCLR), however after some homework I now have a PowerShell script that will delete files based on a variable NumberOfDaysRetention targeting your backup location looking for certain files.

You may need to tweak it as I only use BAK and TRN extensions for backups.

Hopefully you will find it useful as do I.

# Number of Days to Retain on the Local Storage
$NumberofDaysRetention = 7

# Location of the Backup Files
$path = '\\YOURBACKUPLOCATION'
$CutOfDate = (Get-Date).AddDays(-$NumberofDaysRetention)

# Search and Remove Full Backup Files
if ((Test-Path $path) -eq "True")
{
$count = (Get-ChildItem $path -include *.bak -recurse | ?{$_.LastWriteTime -lt        $CutOfDate -and !$_.PSIsContainer}).Count

if ($count -eq $null)
{
$count = 0
}

Get-ChildItem $path -include *.bak -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer} | Remove-Item
Write-Host "There were" $count "Full Backup files deleted"
}
else
{
Write-Host "There are no Full Backup files to be deleted"
}

# Search and Remove Transaction Log Files
if ((Test-Path $path) -eq "True")
{
$count = (Get-ChildItem $path -include *.trn -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer}).Count

if ($count -eq $null)
{
$count = 0
}

Get-ChildItem $path -include *.trn -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer} | Remove-Item
Write-Host "There were" $count "Transaction Log files deleted"
}
else
{
Write-Host "There are no Transaction Log files to be deleted"
}

#Example of going through from a root folder i.e. the instance name and from agent

$NumberofDaysRetention = 7

# Location of the Backup Files
$path = '\\YOURBACKUPLOCATION'
$CutOfDate = (Get-Date).AddDays(-$NumberofDaysRetention)

# Search and Remove Full Backup Files
if ((Test-Path $path) -eq "True")
{
$count = (Get-ChildItem -Force $path -include *.bak -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer}).Count

if ($count -eq $null)
{
$count = 0
}

Get-ChildItem $path -include *.bak -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer} | Remove-Item
#Write-Host "There were" $count "Full Backup files deleted"
}

# Search and Remove Transaction Log Files
if ((Test-Path $path) -eq "True")
{
$count = (Get-ChildItem $path -include *.trn -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer}).Count

if ($count -eq $null)
{
$count = 0
}

Get-ChildItem $path -include *.trn -recurse | ?{$_.LastWriteTime -lt $CutOfDate -and !$_.PSIsContainer} | Remove-Item
#Write-Host "There were" $count "Transaction Log files deleted"
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s