Delete Files Older Than X-Days Except Certain Subfolders

As Technet Gallery is retiring so moving the code to Git Hub. (click below github download link for this script)

Delete Files Older Than X-Days Except Certain Subfolders


Hi Readers,

I have written this script as this was requested by someone in this gallery.
This script will delete the files & folders based on the date except the folders mentioned in the exclude variable.
two excelude folders are defined, you can define more as well, just modify the if condition.
I have hashed the line that will be used for deletion.
Remove the hash after checking what all will be deleted & then run the script again, there might be some red errors that folder doesnt exist , ignore those, as it is  deleting the folder & after that files but if folder/ files are older than as folder is deleted first therefore red errors.
Define Path for finding the files/folders for removal ($path variable)
Create C:\scripts (for running the script)
Create C:\scripts\logs (for log file)
Define $exclude for excluding folders from deletion

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s