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
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