Script to delete folder older than 30 days… by Name
- Ben Liebowitz
- 0
- 3582
I had a need, on one of my file servers, to delete some data that was older than 30 days, to clear space. This would need to be a re-occurring task, so what better way to accomplish it than to script it!
Here’s the rub… The folders are each in a YYYYMMDD format (ex: 20170115), yet the content in the folders could be dated other than the folder name itself. For instance, there could be a folder (say 20170626) that is in the future, so we don’t want to delete it, even though the files in the folder could be from a date in the past. And to add complexity, the folders to be deleted were on a MOUNT POINT inside another drive.
I needed to find the folder to delete based on the folder name and not the item date. Also, the data on this file server is also backed up to one of our Isilon storage arrays, as the amount of data is too big for our backup targets to normally handle. Before we delete the folder for 30+ days ago, we want to make sure all the data has been duplicated on the Isilon. We have a script that runs nightly to do this, but in order to make sure the data is 100% up to date on the Isilon, I decided to incorporate a robocopy command in the script. Also, the Isilon storage only allowed specific accounts access, so I had to map a drive first.
I used variables to set the path that the data was located, as well as the destination for the backup on the Isilon. I also included a variable for the robocopy LOG so I could put the date into the log file name, so there was a new log each day.
Once it gathered the folder name to delete, it runs the robocopy, and once complete, deletes the folder and all it’s subfolders/files. As this is a file server cluster and I run the script via task scheduler on both nodes, I included an IF statement to check to see if the drive exists before proceeding. At the end, it sends an email and includes the before & after free space.
############################################################## # # PS Script to delete file server folders older than 31 days # written by BLiebowitz on 2/6/2017 # ############################################################## # Lets set some variables to start with # Get the date older than 31 days, in the correct format of yyyyMMdd $date = get-date -Date (get-date).AddDays(-31) -Format "yyyyMMdd" # The folder location and destination $path = "D:\data\2017" $destination = <a href="file://\\Isilon\backup">\\Isilon\backup </a># Variables for Space calculation $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}} $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}} $FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}} # Variable to check if drive exists $driveletterexists = Test-Path -path D: # Check if T drive exists, if not, end script if ($DriveLetterExists) { # Find the folder older than 31 days $folder = Get-ChildItem -Path $path -Force | Where-Object { $_.Name -eq $date } # Combine the path/destination and folder into one variable $finalpath = $path + "\" + $folder.Name $finaldestination = $destination + "\" + $folder.Name # Combine the log file path and name with the folder name $log = "d:\scripts\Data_Backup_" + $folder.Name + "_Isilon.log" $removelog = "d:\scripts\Data_Backup_" + $folder.Name + "_deletion.txt" # Find free space before folder deletion add-content $removelog "Free Space Before Deletion:" $volumes = Get-WmiObject -computer "localhost" win32_volume | Where-object {$_.DriveLetter -eq $null -and $_.Label -eq "Mount1"} $volumes | Select Label, $FreeGB, $FreePerc | out-file $removelog # Display Folder Name to be deleted and write to log (before robocopy and delation) write-host $folder is going to be deleted add-content $removelog "$folder is going to be deleted. $(get-date -format g)" # connect to Isilon using domain account for permissions new-psdrive -Name Isilon -psprovider FileSystem -Root $finaldestination # Run robocopy to make sure the data is all backed up invoke-command -scriptblock {robocopy.exe $finalpath $finaldestination /E /COPYALL /COPY:DAT /V /XA:H /LOG:$log /Z /R:10 /W:30} # Delete the folder and it's contents $folder | remove-item -force –recurse # Export report of folder to be deleted including END time Add-Content $removelog "$folder has been deleted. $(get-date -format g)" # Fine free space after deletion add-content $removelog "Free Space After Deletion:" $volumes = $null $volumes = Get-WmiObject -computer "localhost" win32_volume | Where-object {$_.DriveLetter -eq $null -and $_.Label -eq "Mount1"} $volumes | Select Label, $FreeGB, $FreePerc | out-file $removelog -Append # Who to send mail to $recipients = "Ben liebowitz <ben@organization.com>" # Send Mail message for notification send-mailmessage -from "File Server<noreply@organization.com>" -to $recipients -subject "File Server Cleanup - $folder Deleted" -attachments $removelog -body "$path\$folder was Deleted at $(get-date -format g)" -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer smtp.organization.com } else { EXIT }
I hope someone finds this helpful as it took me a while to figure out.
Thanks,
Ben Liebowitz, VCP, vExpert
NJ VMUG Leader