0
0
Read Time:3 Minute, 0 Second
Clear Temp folder (older than 30 days)
This script will clear the temp folder (Including empty sub folders and files ) older than 30 days from all the domain controller across your enterprise. Clearing the temp folder is recommended weekly on all Domain Controllers for optimal performance. This script may be scheduled to run from your “Jump server” or any domain controllers. If you require any further information, please feel free to update in the comment section below.
<# .SYNOPSIS Clear the Temp Files from all the DCs older than 30 days .DESCRIPTION This Script will clear all the files in the c:\temp folder which is older than 30 Days Get the Server list from the Add_DCs.txt Files .NOTES Visit www.shinishonline.com #> #Inputfile containing the list of Domain controllers $DClist = get-content .\All_DCs.txt # set the No of days $limit = (Get-Date).AddDays(-30) #remove old files, if exists Remove-item .\Report-deleted.html -ea SilentlyContinue #Create new HTML File New-Item .\Report-deleted.html -Force #Counters [int]$filcount = 0 [int]$FolCount = 0 #Add HTML Tags $Countfiler = "<HTML> <BODY>" $Countfiler | Out-File -filepath .\Report-deleted.html -append # Loop begins here foreach ($DC in $DClist){ #Report the files Get-ChildItem \\$Dc\c$\Temp -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } #Delete files older than 30 Days Get-ChildItem \\$Dc\c$\Temp -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit} | ForEach-Object{$FilCount++ ; $_.fullName} | Remove-item -Force -ea SilentlyContinue #Delete Empty folders older than 30 days Get-ChildItem \\$Dc\c$\Temp -Recurse -Force | Where {$_.PSIsContainer -and @(Get-ChildItem -Lit $_.Fullname -r | Where {!$_.PSIsContainer}).Length -eq 0 -and $_.CreationTime -lt $limit} | ForEach-Object{ $FolCount++; $_.fullName} | Remove-Item -recurse -ea SilentlyContinue #Add Content to the Report $Countfiler = '<font face="Arial"size="2">' $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = 'Domain Controller : <font color="NavyBlue"> <B>' + $DC + "</B> </font>" $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = "<p></p> " $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = "Total Folders Deleted : " + $FolCount $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = "<p></p> " $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = "Total Files Deleted : " + $filcount $Countfiler | Out-File -filepath .\Report-deleted.html -append $Countfiler = "<BR> </BR> </Font>" $Countfiler | Out-File -filepath .\Report-deleted.html -append } $Countfiler = "</BODY></HTML>" $Countfiler | Out-File -filepath .\Report-deleted.html -append #email the report $strDate = get-date -uFormat "%m-%d-%Y" $strMailSubject = "Temporary Files Cleanup Report - $strDate" [string]$body = Get-Content -Path .\Report-deleted.html Send-MailMessage -Body $body -BodyAsHtml -to "yourgroup@yourcompany.com" -SmtpServer "smtp.yourcompany.com" -From "mailer@yourcompany" -Subject $strMailSubject Write-Host "Script competed" -ForegroundColor Green