Solved

PowerShell question delete files by date

Posted on 2016-08-19
2
42 Views
Last Modified: 2016-08-25
I have a folder called MAX that has files in it and subdirectories in it.   I need a powershell script that deletes files older than 15 days that are located in folder max but does not touch any of the subdirectories or any of the files in the subdirectories in MAX.
0
Comment
Question by:jamesmetcalf74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 41763125
In test mode, remove the -WhatIf to run it for real.
PS 3.0 or later:
$CutOff = (Get-Date).AddDays(-15)
Get-ChildItem C:\Test\Max -File | ? {$_.LastWriteTime -lt $CutOff} | Remove-Item -WhatIf

Open in new window

PS 2.0:
$CutOff = (Get-Date).AddDays(-15)
Get-ChildItem C:\Test\Max | ? {!$_.PsIsContainer -and ($_.LastWriteTime -lt $CutOff)} | Remove-Item -WhatIf

Open in new window

0
 
LVL 10

Assisted Solution

by:Tim Edwards
Tim Edwards earned 250 total points
ID: 41763131
This is the code that I have a scheduled task to cleanup backup files older than x amount of days and it works well
#Delete Mitel Controller Backups older than 3 Days#

#defined Parameters#

$Date = Get-Date
$Days = "3"
$TargetDrive = "M:\"

#defined extension#

$Extension = "*.tar"

#define LastWriteTime based on number of days#
$LastWrite = $Date.AddDays(-$Days)

#get files based on lastwrite filter and specified folder#
$Files = Get-Childitem $TargetDrive -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "DarkRed"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question