About Me

My photo
a Dynamic and Energetic guy.....

Thursday, May 8, 2014

Delete files using PowerShell based on Created Time

We have to clean our SharePoint backup location after 7 days.
We have decided to create a POWERSHELL script and run it as a WINDOWS TIMER.
It is simple.

The PowerShell Script,

# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = [int]1 # 30 days
foreach ($path in Get-Content "pathList.txt") {
# Write information of what it is about to do
Write-Host "Trying to delete files older than $oldTime days, in the folder $path" -ForegroundColor Green
# deleting the old files
Get-ChildItem $path -Recurse -Include "*.PNG", "*.bak" | WHERE {($_.LastWriteTime  -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force
}



there we have set 3 parameters.
Filepath   = the folder that we needs to clean
DAYS    = deleting files older than this DAYS
Types     = the file types that we need to delete

the "pathList.txt" has all the File paths that we have to delete.

 


We can delete SELECTED file types.


After we run the script, we can see all the FILES TYPES (.png) been deleted if OLDER than specify DAYS.

We can set a windows timer service and execute the .ps1 file



No comments:

My Masters