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:
$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.