##########Powershell Temp File Cleanup v0.7 By Blakem17#################################### ############################################################################################ ############################################################################################ ##################### User Defined Variables################################################ $companyName = "" $compName = "$env:computername.$env:userdnsdomain" $emailTo = "" #The email you wish to send to $emailFrom = "" #The email you wish to send from $smtpServer = "" #The Smtp server you wish to send from #########################Calculate Current Space on Drive############################### $curDskString = fsutil volume diskfree c: | Out-String $curDskstringSp = $curDskString.split(':')[1] $curDiskIntSpaceSt = $curDskstringSp -replace'[a-z#" "]' $curDiskIntSt = $curDiskIntSpaceSt.TrimEnd("`n") $curDiskIntGB = $curDiskIntSt / 1000000000 #####Current Free Space On Drive in GB######## #################Users########################################################## $uListRaw = Get-ChildItem C:\users | ?{ $_.PSIsContainer } | Select-Object FullName $uLString = $uListRaw | Out-String $uLStringNa = $uLString -replace("C:\\users"),"" $uLStringNb =$uLStringNa -replace("FullName"),"" $uLStringNc = $uLStringNb -replace("-"),"" $uLStringNd = $uLStringNc -replace(" "),"" $uLStringNe = $uLStringNd -replace("\\")," " $ulStringNf = $uLStringNe.trimstart() $uLStringNg = $uLStringNf -replace(" "),"," $uLStringNh = $uLStringNg -replace("`n"),"" $uLStringNi = $uLStringNh -replace("`r"),"" $uLStringNj = $uLStringNi.TrimEnd() $uList = $uLStringNj.TrimStart() $users = $uList -split "," ############################Defining Cleanup Locations#################################### $loc1 = 'C:\Temp' $loc2 = "$env:SystemRoot\temp" $loc3 = "C:\users" $loc4 = "AppData\Local\temp" $loc5 = "AppData\Local\Microsoft\Windows\Temporary Internet Files" $loc6 = "AppData\Roaming\Microsoft\Windows\Cookies" $loc7 = "AppData\Roaming\Microsoft\Windows\Recent" $loc8 = "AppData\Local\CrashDumps" ####################################General Cleanup########################################## Remove-Item -Recurse -Force $loc1 Remove-Item -Recurse -Force $loc2 ###############################User Cleanup####################################################### ForEach($user in $users){$locN1 = $loc3 + "\" + $user + "\" + $loc4 Remove-Item -Recurse -Force $locN1 } ForEach($user in $users){$locN2 = $loc3 + "\" + $user + "\" + $loc5 Remove-Item -Recurse -Force $locN2 } ForEach($user in $users){$locN3 = $loc3 + "\" + $user + "\" + $loc6 Remove-Item -Recurse -Force $locN3 } ForEach($user in $users){$locN4 = $loc3 + "\" + $user + "\" + $loc7 Remove-Item -Recurse -Force $locN4 } ForEach($user in $users){$locN5 = $loc3 + "\" + $user + "\" + $loc8 Remove-Item -Recurse -Force $locN5 } ###############################Disk Cleanup Calculation######################################### $cleanDskString = fsutil volume diskfree c: | Out-String $cleanDskstringSp = $cleanDskString.split(':')[1] $cleanDiskIntSpaceSt = $cleanDskstringSp -replace'[a-z#" "]' $cleanDiskIntSt = $cleanDiskIntSpaceSt.TrimEnd("`n") $cleanDiskIntGB = $cleanDiskIntSt / 1000000000 $mbCleaned = ($cleanDiskIntSt - $curDiskIntSt) / 1000000 "After cleaning the Free space on the drive is $cleanDiskIntGB Gigabytes. $mbCleaned mb has been Cleaned" #############################Email Report######################################################## send-mailmessage -to $emailTo -subject "Cleanup on $CompanyName $CompName" -from $emailFrom -smtpserver $smtpServer -body "After cleaning the Free space on the drive is $cleanDiskIntGB Gigabytes. $mbCleaned mb has been Cleaned"