I'm currently using the Powershell script below to delete all files directories and sub directories of "$dump_path" that are seven days or older based upon the creation date and not modified date.

The problem with this script is that if folder "A" is seven (or more) days old it will be deleted even if its sub folders and files are less then seven days old.

What I would like this script to do is delete all files from the root and in all sub folders of "$dump_path" that are seven or more days old but maintain the parent folder(s) of files and folders that are less than seven days old even if that means the parent folders are more than seven days old. If all subfolders and files are seven days or older than the parent folder then the parent can be deleted.

Slightly obscure problem I know, but the intention is to have a 7 day retention period of all data in a 'sandbox' location of our shared areas.

Also, an added bonus if it could generate a log of what it deletes and e-mails it out post deletion.

# set folder path
$dump_path = "c:\temp"

# set minimum age of files and folders
$max_days = "-7"

# get the current date
$curr_date = Get-Date

# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)

# delete the files and folders
Get-ChildItem $dump_path   | Where-Object { $_.CreationTime -lt $del_date }  | Remove-Item -Recurse
  • Any particular reason for you to unaccept my answer after more than 6 months? – Ansgar Wiechers May 16 '13 at 13:02

May I suggest a slightly different approach? I'd delete all files older than 7 days first, and in a second step delete empty folders. Something like this:

$deleted = @()

Get-ChildItem $dump_path -Recurse | Where-Object {
  -not $_.PSIsContainer -and $_.CreationTime -lt $del_date
} | ForEach-Object {
  $deleted += $_.FullName
} | Remove-Item

function Remove-EmptyFolders($folder) {
  Get-ChildItem $folder | Where-Object { $_.PSIsContainer } | ForEach-Object {
    $path = $_.FullName
    Remove-Emptyfolders $path
    if ( @(Get-ChildItem $path -Recurse | Where-Object { -not $_.PSIsContainer}).Length -eq 0 ) {
      $deleted += $path
      Remove-Item $path -Recurse -Force

Remove-EmptyFolders $dump_path

$html = $deleted | Select-Object @{Name='Path';Expression={$_}} | ConvertTo-Html
  • I'd use an array, append the paths of the deleted objects to it, and then convert the array to HTML. See edited post (you need Select-Object to create objects from the strings, so ConvertTo-Html can work with them). For sending e-mail from PowerShell see this blog post. – Ansgar Wiechers Sep 9 '12 at 21:02
  • Not sure what you mean. ConvertTo-Html creates a table from the array with one row per per array element. – Ansgar Wiechers Sep 10 '12 at 10:13
  • The script works great! It won't list the folders in the email because you're sending a list of $deleted (which is only used in the deletion of files part of the script) I added a Set-ItemProperty -name IsReadOnly -value $false before the Remove-Item command so read-only files can be deleted as well. – user273450 Feb 26 '15 at 14:25

Thank you so much for the code. It has been a great help.

I added the following to send an email with the deletion results:

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

$html = $deleted | Select-Object @{Name='Path';Expression={$_}} | ConvertTo-Html -Head $style

#SMTP server name
 $smtpServer = "smtp.xxxx.com"

 #Creating a Mail object
 $msg = new-object Net.Mail.MailMessage

 #Creating SMTP server object
 $smtp = new-object Net.Mail.SmtpClient($smtpServer)

 #Email structure 
 $msg.From = "email@xxxx.com"
 #$msg.ReplyTo = "replyto@xxxx.com"
 $msg.subject = "Deleted files and folders"
 $msg.IsBodyHTML = $true
 $msg.body = $html

 #Sending email 

I did notice that the email generated only contained a list of the files that were deleted, not the folders, so haven't worked out why the folders aren't listed (if you have any ideas that would be great).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy