Thursday, September 12, 2013

Get and remove files older than 7 days

Good day
Using this script you will be able to remove all the files in a folder tree that are older than 7 days

cls #Clear the Screen

Set-Location "Location Of the Folder" #Set the Folder Location 

$alldir=ls #Get a list of files and store then in $alldir variable

$removebefore=date #get the current date and store it in $removebefore variable

$removebefore=$removebefore.AddDays(-7) # Before 7 Days

    foreach($directory in $alldir){ #Prepare to read each item in the list
           Get-ChildItem -Path $directory.PSChildName |where{$_.CreationTime -lt $removebefore| Remove-Item
        

        } #Read Each Item and show only the items with CreationTime is before $removebefore " the 7 days"


If you want to know what other property you can use instead of CreationTime, then you will need to get the members list for ls (Get-ChildItem) ls | Get-Member


Hope its clear
if you have any command please write it down, and if you like this post please share it 





No comments: