I am really good at reinventing the wheel! Most of the time the wheel isn’t even better – just more complicated. Back in May, I posted a script that to clean up old files. I used this on Windows Server 2003 box for a few years and moved it to a 2012 R2 server. The script used PowerShell and basically compared the LastAccessTime attribute to some date. A reader named Jeff took one look at my script and basically said, “Joseph – just use File Management Tasks”.
And Jeff was right! File Management Tasks are easier to setup and provide a lot more flexibility out of the box. Today, let’s look at how the File Server Resource Manager can keep our shares clean of old data!
Configuring a File Management Task
First, your file server does have to be running Windows Server 2008 R2 or higher. For those of you kicking it old school, I’ll wait while you upgrade. 🙂
Next, add the File Server Resource Manager role to your server. Accept the defaults during the installation.
Launch the File Server Resource Manager MMC and navigate to File Management Tasks. Select Create File Management Task. Name your task Expire Stale Data. Under scope, select Add. Add any folders/volumes that need to be cleaned up.
Under the Action tab, ensure that Type is set to File Expiration. When a file is marked to expire (more on that in a bit), it is never deleted. Instead, it is moved out of the scope (C:\public in the screenshot above) and into your expiration directory. Don’t worry about accidently deleting files because you can’t do it! In the screenshot below, I am moving expired data to a local folder.
Select the Condition tab. In this window, you can set criteria for what files are considered stale. By creation multiple tasks for different scopes, you can have more restrictive or aggressive cleanups as needed. Each check box functions as an AND statement. Our conditions below ensure that files are not moved they haven’t been modified for 3 years and haven’t been accessed for two years.
If desired, you can use the Notification tab to alert users of pending changes. In your notification, you can include a list of files that will be removed and provide additional instructions to your staff on what they should do. You can use the Report tab to email IT staff members a list of changes after a task has completed.
For your task to run, it must have a schedule. Press the Schedule Tab – then Edit. I recommend a schedule of once per month at the most. You can run it now for testing or wait until the schedule. As it is running, you can watch your Expired Files Directory populate. To make restoration easier, the files are moved to the expired directory using their previous folder structure. From here, you can schedule a permanent deletion by using a schedule task to empty the folder. You can also use this PowerShell script:
1 |
$Date = (Get-Date).AddMonths(-6) | get-date -UFormat "%Y-%m" $ExpiredDirectory = Get-ChildItem "C:\Expired Files\FQDN of Server" | where name -like "Expire Stale Data_$Date*" $ExpiredDirectory | Remove-Item -Recurse -whatif |
Adjust the .AddMonths(-6) to change how long you wish to keep folders in the Expired Directory. This script is designed to be ran monthly and will delete the folder that is six months old.
I set this up using these instructions and ran it on a set of test folders. The process works for files but it leaves the folders in place. How do I expire the folders too?
I should expire folders as well. Check to make sure that the folder modified dates aren’t being updated some how (such as through an antivirus scan or a backup program).
Hi John,
Were you able to figure this out? I have the same issue. Running it on Windows Server 2016. Ran the task on a test folder and it moved the older files out to the expiration directory keeping the directory structure however the old folders in the original directory are still there. It never deleted the older folders so I now have all these empty folders. The oddest part is now all of the folders have a new creation date of when the task ran.
Thanks!
No problem!
Hi,
I’m trying to implement this to keep some backup files under control. We only need to keep a weeks worth of zip files but it’s not moving the files on test runs. I’ve tried various conditions and I can see in the little report tree at the bottom of the MMC while it runs that it processes 15 files (the amount in the directory) but for some reason its not moving the files. Any ideas at all?
Did you ever get this one figured out?
Hi Joseph,
Thanks for getting back. Unfortunately, no 🙁 It runs but never moves any files. I’ve tried various conditions combinations and it ‘processes’ the files but never shifts them over. Are there any specific permissions I need to be aware of? Haven’t really found much that’s been useful to me on my searches so far!
Could you run the task as SYSTEM?
Hi,
Just dug out the task and it’s already running as SYSTEM with the Highest Privileges as well.
Hi,
I have configured quoata with File Server Resource Manager, but when i test send email i have error:
Relay denied proper adhentification required.
I have an another server that send quota email, i tested it with this smtp that not working in the first server.
This work fine with the second.
i think there a service or role that must be instaled in the frist server to be able to send email.!!
Thanks
What kind of mail server are you using to relay? You will need to allow the IP/device to relay messages on your mail server.
Thanks, for this post from…a year ago. I was wondering if there is a way to have exemptions in the purge. So, if I have a Users share with folders for Peter, Paul, Mary and Joe, and Joe is a hoarder, how can I exclude his folder and files? Can I still set it up on the Users share or now do I have to do it individually on Peter, Paul and Mary?
Thanks,
Matt
Hey Matt – I don’t know of a way with this method. My older method had exclusions though. See this link: http://deployhappiness.com/cleaning-and-deleting-old-files-with-powershell/
Thanks for you response… I think I figured out a way, but, still need to test it. I will try creating Classification-Ordered Lists and then setup the Management Tasks based on the Classification Property Conditions. So Low properties will purge after 2 years, Medium after 7, and High never. I hope I made sense. And, I hope it works.
What kind of actions can you perform from here? I’m envisioning leveraging this with some sort of Powershell script that moves the files to an equivalant folder on an “Archive” file server on slower storage.
Here is a list of actions that should help you: http://technet.microsoft.com/en-us/library/dd759227.aspx
Let me know how you modify it!
Glad you found it! We also use it for monitoring shares for the presence of things like MP3/AVI and other “non-business” related files. You can use it in sneaky mode (report only) or full on agressive (prevent them saving).
We also have it applying quota’s to user home folders, warning them when they’re getting close, granting a temporary extension and then applying a hard quota.
It’s possibly one of the most underutilised features in windows!
Thank you for pointing me in the right direction! If you ever get some free time, think about writing a guest post (http://deployhappiness.com/start-here/guest-posting/). It sounds like you could teach us all some nice tricks!