Solved

PowerShell question delete files by date

Posted on 2016-08-19
2
52 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

729 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