Solved

PowerShell question delete files by date

Posted on 2016-08-19
2
36 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
2 Comments
 
LVL 82

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 8

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now