Use Powershell to periodically cleanup temporary dirs

Simple code snippet to cleanup files older than n days. 


#json extraction to variable set
$Dirs=(Get-Content -Raw -Path cleanup.json | ConvertFrom-Json)

#.json file example:
#	{"DIR":"C:\\Archives\\Dir1","DAYS":"20"},
#	{"DIR":"C:\\Archives\\Dir2","DAYS":"20"},
#	{"DIR":"C:\\Archives\\Dir3","DAYS":"20"}

foreach ($Dir in $Dirs){
    $Log = $Dir.DIR
    Get-ChildItem -Path ($Dir.DIR) -Force | Where-Object {$_.LastWriteTime -lt (Get-Date).AddMinutes(-$Dir.DAYS)} | Remove-Item -Force -Recurse -Exclude *.txt -Verbose 4>&1 | Add-Content FTP_cleanup.log

Remember to create cleanup.json in the same directory.

Author of this script: Adrian Ligiewicz (hire this Powershell magician if you need to automate some Windows stuff, he’s goooood)