The script I am putting out here today is named “Remove-OldFiles”. As the name implies, this script will search out and remove files (with a given extension, i.e. .mkv) over a certain age threshold. This script was tested with PowerShell 4.0.
One of the goals for this task was to be robust – I did not want to pin it down to a certain location, file extension, or age. This enabled my organization to setup scheduled tasks to routinely cleanup target files, and also run ad-hoc cleanup tasks.
The script does have a full help file available. You may want to review that help file before running the script. Prior to your first run, you will want to edit lines 40 and 42. These are output locations the script uses for a Log (errors, information etc) and a CSV of removed files. You can use UNC paths for these variables.
If either folder does not yet exist, the script will create them before proceeding. If the user running the script does not have R/W access to the folder, the script will error and exit.
The rest of the script has somewhat decent comments, so you should be able to follow along through the logic.
Once you have your output paths set, you should be ready to run it. If you are wanting to run the script without actually removing the files found, use the -WhatIf parameter. This will run the script with your parameters, generate all logs and CSV output, but not truly remove the files.
To execute the script, you need to specify some parameters. Here is a complete list of the parameters:
- age (-a): specifies the file age, in days
- fileext (-file): specifies the file extension to search for. When entering this parameter, simply enter the letters (mkv) not the preceeding dot (.mkv)
- Searchpage (-s): specifies the path to search within. This is recursive (searches all subfolders). This can be a local disk (C:\folder\) or a remote server (\\servername\share\folder\)
- Folders (-folders): searches for empty folders instead of a specific file type. If -folders is being used, age and fileext should not be used
- WhatIf (-WhatIf): executes the script, running all commands (including log and CSV output) in PowerShell “WhatIf” mode – no files/folders will actually be removed.
This script can also be setup to run on a regular interval, using Microsoft’s built-in Task Scheduler. Here’s how (steps written for Server 2012 / Win 7+):
- Open Control Panel –> Administrative Tools -> Task Scheduler
- Click “Create Task” in the left panel (“Create Basic Task” may also work)
- Give your Task a name (required) and a description (not required)
- If you’re doing “Create Task”, here you will have options to select the user to run the task under. Your default user may not have the required permissions, so this may need changed.
- Click to the Triggers tab, and click “new”. This is where you will establish the schedule for execution of the script. Make sure “Enabled” is checked at the bottom of this screen as well.
- After your Trigger has been set, click the Actions tab, and click New
- You “Action” will be “Start a Program”
- The “Program/Script” field should only have “powershell” (no quotes) in it
- The Add Arguments field you need to specify several things
- -file : this is path to the .ps1 file (i.e -file C:\temp\Remove-OldFiles.ps1)
- -a, -file, -s and -folders parameters, as needed to execute the script as you want
- Once done with that, you can tab through the Conditions and Settings tabs, but the defaults should work for a lot of people.
- Click OK at this point – you may be prompted for a username and password. Enter your credentials, and you should have a saved, scheduled task.
- Ensure that the task shows “Ready”, under the Status column. If it shows “Disabled”, right click it and select “Enable”
If you have any questions, issues or suggestion (I’m *always* open to all three, especially if I can improve myself), email me: email@example.com!