# Script to look at folders and delete old items
$logfile = "c:\folder\logfilename.txt"
$date = get-date
$allfilestodelete
# Set variables for the timeframes used to determine stale files
$1wlimit = ($date).AddDays(-8)
$1mlimit = ($date).AddDays(-32)
$6mlimit = ($date).AddDays(-187)
$1ylimit = ($date).AddDays(-366)
# Test Log File Path, create a log file if it doesn't exist
$logfilePath = (Test-Path $logFile)
if (($logFilePath) -ne "True")
{
# Create File
New-Item $logfile -ItemType File
Add-Content $logfile "Log file created $date "
}
Add-Content $logfile "AutoDelete script starting to run on: $(get-date) "
# This is the prefixes that we will use to mate with the suffixes to set all the folders that we are looking at
$siteuncprefixes = "\\server1\share\","\\server2\share\","\\server3\share\"
# These are the suffixes, so we will end up with \\server1\share\foldername\1weekfolder,
# \\server1\share\foldername\1monthfolder and so on...
$1wuncsuffix = "foldername\1weekfolder"
$1muncsuffix = "foldername\1monthfolder"
$6muncsuffix = "foldername\6monthsfolder"
$1yuncsuffix = "foldername\1yearfolder"
# Main loop to look at each site's folders and check for stale files
foreach ($site in $siteuncprefixes){
Add-Content $logfile "Scanning $site for expired files at $(get-date) "
$currentpath = $site + $1wuncsuffix
#Add-Content $logfile "Scanning $currentpath for 1 week expired files at $(get-date) "
$1wfilestodelete = Get-ChildItem -Path $currentpath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.lastwriteTime -lt $1wlimit -and $_.creationtime -lt $1wlimit }
$currentpath = $site + $1muncsuffix
#Add-Content $logfile "Scanning $currentpath for 1 month expired files at $(get-date) "
$1mfilestodelete = Get-ChildItem -Path $currentpath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.lastwriteTime -lt $1mlimit -and $_.creationtime -lt $1wlimit }
$currentpath = $site + $6muncsuffix
#Add-Content $logfile "Scanning $currentpath for 6 month expired files at $(get-date) "
$1mfilestodelete = Get-ChildItem -Path $currentpath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.lastwriteTime -lt $6mlimit -and $_.creationtime -lt $1wlimit }
$currentpath = $site + $1yuncsuffix
#Add-Content $logfile "Scanning $currentpath for 1 year expired files at $(get-date) "
$1yfilestodelete = Get-ChildItem -Path $currentpath -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.lastwriteTime -lt $1ylimit -and $_.creationtime -lt $1wlimit }
$allfilestodelete = $allfilestodelete + $1wfilestodelete + $1mfilestodelete + $6mfilestodelete + $1yfilestodelete
}
# Count the total number of files and write to the log file
$count = $allfilestodelete.fullname.count
Add-Content $logfile "Found $count expired files to delete at all sites: "
# Write to the log all items to be deleted
$allfilestodelete | select-object -property fullname, lastwritetime | Ft -autosize | out-string -width 4096 | add-content $logfile
# Delete expired files
$allfilestodelete.fullname | Remove-Item -Force
# Write to the log file that it's done
Add-Content $logfile "AutoDelete script finished runing on: $(get-date) "
Wednesday, February 1, 2017
Expired Files Deletion Script
This is a script that checks through a list of folders based on a standard set of folders at multiple sites and purges old files. Nothing too fancy here.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment