Script to remove data from a share monthly.

  • 23 November 2022
  • 2 comments
  • 148 views

Userlevel 7
Badge +8

I have a lot of users that require a location accessible to everyone to share files on a temporary basis. (non confidential type info)

 

My issue is that nobody likes to delete anything here… EVER! After having to clean it out multiple times I figured I’d run a scheduled task for a PowerShell script to clean the folder out on the first of every month. 

 

The issue is now people wondering why the files were deleted event though it was explained and called Temporary_Data_Transfer. lol

 

I created a folder titled  “_Instructions” at the top that won’t be removed by the script and the underscore keeps it at the top.  There is a document in it so users can review the date the folder is cleaned out and states that files will be deleted monthly. This has really helped clear things up with people not knowing their files will be removed. 

The script omits clean up of that folder by increasing the date to make it appear as a new file. 

 

The also script keeps a log of deleted files so when someone is asking I can quickly see if it was removed and do a restore in our backups. 

 

Time frames can be adjusted on the logs, and you could add a buffer with the $NumberOfDaysBeforeDeletion field.  Being a scheduled monthly task it seems to be working fantastic the way it is.

# Make sure the folder where the output file is saved to has been created beforehand
$Folder = "F:\Temporary_Data_Transfer\"
$NumberOfDaysBeforeDeletion = 0
$OutputFileLocation = "F:\Temporary_Data_Transfer_Logs\$(get-date -f yyyy-MM-dd)-FilesDeleted.txt"

# Update the date and time on the Instructions file, so it won't be deleted
foreach($file in Get-ChildItem F:\Temporary_Data_Transfer\_Instructions\) {$(Get-Item $file.Fullname).lastwritetime=$(Get-Date)+1}

#Delete files older than days specified
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$NumberOfDaysBeforeDeletion)} |
ForEach-Object {
$_ | Remove-Item -Force -Recurse
$_.FullName | Out-File $OutputFileLocation -Append
}

#Delete empty folders and subfolders
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {$_.PsIsContainer -eq $True} |
? {$_.getfiles().count -eq 0} |
ForEach-Object {
$_ | Remove-Item -Force -Recurse
$_.FullName | Out-File $OutputFileLocation -Append
}

# Clean up the log file folder to delete log files after they are two weeks old.

$LogFolder = "F:\Temporary_Data_Transfer_Logs\"
$NumberOfDaysBeforeDeletelogs = 90

#Delete log files older than days specified
Get-ChildItem $LogFolder -Recurse -Force -ea 0 |
? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-$NumberOfDaysBeforeDeletelogs)} |
ForEach-Object {
$_ | Remove-Item -Force -Recurse
$_.FullName
}

#Delete empty folders and subfolders in the main log folder
Get-ChildItem $LogFolder -Recurse -Force -ea 0 |
? {$_.PsIsContainer -eq $True} |
? {$_.getfiles().count -eq 0} |
ForEach-Object {
$_ | Remove-Item -Force -Recurse
$_.FullName
}

 


2 comments

Userlevel 7
Badge +20

Very cool script Scott.  Might have to borrow this to use on some of our repos.  😂

Userlevel 7
Badge +8

Very cool script Scott.  Might have to borrow this to use on some of our repos.  😂

I’ve been backing it up for a while but now that people understand it, I don’t have to back up this share anymore. (I still do but if someone asks I say no). When they call me upset I say I magically found their files :)

 

It’s taken a few years but the culture is slowly changing here and people realize they don’t need to save EVERYTHING they touch. especially when we have multiple copies of it. 

Comment