?
Solved

PowerShell question delete files by date

Posted on 2016-08-19
2
Medium Priority
?
56 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 85

Accepted Solution

by:
oBdA earned 1000 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 1000 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

Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

800 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