Advertisement
blakem17

Powershell Temp File Cleanup v0.7

Sep 22nd, 2015
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########Powershell Temp File Cleanup v0.7  By Blakem17####################################
  2. ############################################################################################
  3. ############################################################################################
  4. ##################### User Defined Variables################################################
  5. $companyName = ""
  6. $compName = "$env:computername.$env:userdnsdomain"
  7. $emailTo = "" #The email you wish to send to
  8. $emailFrom = "" #The email you wish to send from
  9. $smtpServer = "" #The Smtp server you wish to send from
  10.  
  11. #########################Calculate Current Space on Drive###############################
  12. $curDskString   = fsutil volume diskfree c: | Out-String
  13. $curDskstringSp = $curDskString.split(':')[1]
  14. $curDiskIntSpaceSt = $curDskstringSp -replace'[a-z#" "]'
  15. $curDiskIntSt = $curDiskIntSpaceSt.TrimEnd("`n")
  16. $curDiskIntGB = $curDiskIntSt / 1000000000 #####Current Free Space On Drive in GB########
  17.  
  18. #################Users##########################################################
  19. $uListRaw = Get-ChildItem C:\users | ?{ $_.PSIsContainer } | Select-Object FullName
  20. $uLString   = $uListRaw | Out-String
  21. $uLStringNa = $uLString -replace("C:\\users"),""
  22. $uLStringNb =$uLStringNa -replace("FullName"),""
  23. $uLStringNc = $uLStringNb -replace("-"),""
  24. $uLStringNd = $uLStringNc -replace(" "),""
  25. $uLStringNe = $uLStringNd -replace("\\")," "
  26. $ulStringNf = $uLStringNe.trimstart()
  27. $uLStringNg = $uLStringNf -replace(" "),","
  28. $uLStringNh = $uLStringNg -replace("`n"),""
  29. $uLStringNi = $uLStringNh -replace("`r"),""
  30. $uLStringNj = $uLStringNi.TrimEnd()
  31. $uList = $uLStringNj.TrimStart()
  32. $users = $uList -split ","
  33. ############################Defining Cleanup Locations####################################
  34. $loc1 = 'C:\Temp'
  35. $loc2 = "$env:SystemRoot\temp"
  36. $loc3 = "C:\users"
  37. $loc4 = "AppData\Local\temp"
  38. $loc5 = "AppData\Local\Microsoft\Windows\Temporary Internet Files"
  39. $loc6 = "AppData\Roaming\Microsoft\Windows\Cookies"
  40. $loc7 = "AppData\Roaming\Microsoft\Windows\Recent"
  41. $loc8 = "AppData\Local\CrashDumps"
  42.  
  43. ####################################General Cleanup##########################################
  44. Remove-Item -Recurse -Force $loc1
  45. Remove-Item -Recurse -Force $loc2
  46. ###############################User Cleanup#######################################################
  47. ForEach($user in $users){$locN1 = $loc3 + "\" + $user + "\" + $loc4
  48. Remove-Item -Recurse -Force $locN1
  49. }
  50. ForEach($user in $users){$locN2 = $loc3 + "\" + $user + "\" + $loc5
  51. Remove-Item -Recurse -Force $locN2
  52. }
  53. ForEach($user in $users){$locN3 = $loc3 + "\" + $user + "\" + $loc6
  54. Remove-Item -Recurse -Force $locN3
  55. }
  56. ForEach($user in $users){$locN4 = $loc3 + "\" + $user + "\" + $loc7
  57. Remove-Item -Recurse -Force $locN4
  58. }
  59. ForEach($user in $users){$locN5 = $loc3 + "\" + $user + "\" + $loc8
  60. Remove-Item -Recurse -Force $locN5
  61. }
  62.  
  63. ###############################Disk Cleanup Calculation#########################################
  64.  
  65. $cleanDskString = fsutil volume diskfree c: | Out-String
  66. $cleanDskstringSp = $cleanDskString.split(':')[1]
  67. $cleanDiskIntSpaceSt = $cleanDskstringSp -replace'[a-z#" "]'
  68. $cleanDiskIntSt = $cleanDiskIntSpaceSt.TrimEnd("`n")
  69. $cleanDiskIntGB = $cleanDiskIntSt / 1000000000
  70. $mbCleaned = ($cleanDiskIntSt - $curDiskIntSt) / 1000000
  71. "After cleaning the Free space on the drive is $cleanDiskIntGB Gigabytes. $mbCleaned mb has been Cleaned"
  72. #############################Email Report########################################################
  73.  
  74. 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"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement