PowerShell: Delete old files based on a given retention period (days) #maintenance

How to delete old files based on a given retention period (days) with PowerShell?
Well…it’s easy! And you might want to use it to clean-up a few old backup files (or even *.csv *.sql or *.txt).

Basically it consists of two functions, namely ‘DeleteOldFilesWithExtension‘ and ‘DeleteOldFiles‘ (without looking for certain extensions):

PowerShell-delete-old-files

Code:
———————————————————————————————

function global:DeleteOldFilesWithExtension([string]$Path, [int]$Daysback, [string]$Extension)
{ #Begin Function

#$Daysback = "-7"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
#$Path = "D:\PowerShell\SQLScripts"

#Based on Creation Date:
#Get-ChildItem $Path -Include $Extension -Recurse | Where-Object { $_.CreationTime -lt $DatetoDelete } | Remove-Item -Confirm:$false -force -recurse

#Based on Modified Date:
Get-ChildItem $Path -Include $Extension -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Confirm:$false -force -recurse

} #End Function

function global:DeleteOldFiles([string]$Path, [int]$Daysback)
{ #Begin Function

#$Daysback = "-7"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
#$Path = "D:\PowerShell\SQLScripts"

#Based on Creation Date:
#Get-ChildItem $Path -Recurse | Where-Object { $_.CreationTime -lt $DatetoDelete } | Remove-Item -Confirm:$false -force -recurse

#Based on Modified Date:
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Confirm:$false -force -recurse

} #End Function

DeleteOldFiles "D:\PowerShell\SQLScripts" -7
DeleteOldFilesWithExtension "D:\PowerShell\BackupScripts" -7 "*.sql"

———————————————————————————————


The bottom two lines are actually calling/executing the functions:
(FunctionName) (Path/Folder) (RetentionPeriodInDays) (ExtentionType – optional)

So save the scripts noted above as ‘DeleteOldFiles.ps1‘ and you’re able to execute it via a SQL Agent Job or from the command line.

How to execute?:
Execute this statement in Command (CMD) or via a SQL Agent Job (PowerShell step):

sql-agent-powershell-step-delete-old-files

PowerShell "E:\PowerShell\DeleteOldFiles.PS1"


Other PowerShell scripts
Script all SQL database objects with PowerShell?
https://clinthuijbers.wordpress.com/2012/09/06/use-powershell-to-script-all-database-objects/

More things you could do with PowerShell? Click here.








Advertisements

One thought on “PowerShell: Delete old files based on a given retention period (days) #maintenance

  1. Pingback: SSIS: Delete old files with PowerShell (via a ‘Execute Process Task’) #FileRetentionPeriod | Clint Huijbers' Blog

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