Automatically Delete Older Folder Contents with Powershell on Windows 7

Thursday, January 26, 2012 posted by CSch

The aim of this guide is to create a folder whose content is deleted if the contained files have not been accessed longer than a specific time (this process is applied to single files, not the whole folder). I will choose two weeks for demonstration purposes (= 14 days). Such a folder can be used as temporary folder of any kind, be it for downloaded files/installers or to just keep the desktop clutter-free.
This can be achieved with a combination of PowerShell script and Windows task scheduler. The folder that I will use for this will be C:\Users\howtoforge\Desktop\Temp and is located on my desktop for easy access. To keep order to it, create another folder for your custom scripts if you haven't already got one, mine will be C:\Scripts.
Open a new instance of notepad and save it in your scripts folder as delete_temp.ps1. .ps1 is the file extension for PowerShell scripts. Now enter following into the script:

cd "C:\Users\howtoforge\Desktop\Temp";
Get-Childitem | Foreach-Object {if ($_.LastAccessTime -le (get-date).adddays(-14)) {remove-item -recurse -force $_}};

Save the script again. What it does: the script changes into the directory that we want to observe, looks at its items and then deletes every one whose last access time is older than 14 days recursively (it only looks at the items directly placed in the folder, not at subdirectories). The time interval is specified in the adddays attribute of the get-date function here (which can also be addmonths, addhours, etc...) and is a negative number to actually subtract the number of days from the present date. You can change it to your likings.
The script being ready, you have to configure PowerShell to enable calling scripts - therefore open an elevated command line (search the menu for cmd, right-click and select Run as administrator). Open PowerShell by entering

powershell

Afterwards, enter

Set-ExecutionPolicy RemoteSigned

to enable calling scripts. Now you can test your script by right-clicking it and selecting Run with PowerShell. If nothing goes wrong (no red text in the flashing window), proceed to schedule the task, otherwise check your script for errors.

To schedule the task, open Control Panel > System and Security > Administrative Tools > Schedule Tasks. On the left pane, select Task Scheduler Library, then right-click the central task-list and select Create New Task.
On the General tab, give the task a name and a description. Furthermore, choose your version of Windows and optionally choose to run it with highest privileges.
The Trigger tab defines what will call the script - hit New and choose one or more of the various possibilities and events. I choose to run the script when the machine goes idle, since the script will blink up in a PowerShell window when called, and I don't want that to disturb my work (although it's really only a split second if you don't delete several GB of files).
On the Actions tab you define what to do - hit New again. Now don't enter the actual script as program to run - this goes to the Add arguments line (enter the full path here). What you need to do is to call the PowerShell executable with the script as an argument. I use PowerShell 1.0 which is located in C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe - enter this path into the Program/script line and hit OK.
Now configure the next two tabs for your needs and hit OK again to create the task.

The selected folder will then be scanned for files that haven't been accessed for longer than the given period every time the task triggers.

Be Sociable, Share!



2 Responses to “Automatically Delete Older Folder Contents with Powershell on Windows 7”

  1. Sam says:

    Is there a way to have it search the subdirectories as well? In my case, I have a parent directory for log files and there are about a dozen subdirectories that hold all these log files. I would like the script to look at the subfolders as well and delete the older files.

  2. Sam says:

    Nevermind, I figured it out. I actually decided to do it like this:

    Get-ChildItem c:\logfiles -force -recurse | where-object {$_.LastWriteTime -le (Get-Date).AddDays(-7) -and !$_.PsIsContainer} | remove-item -force

    Your post got me started in the right direction. Thanks!

Leave a Reply