Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function global:Write-Verbose ( [string]$Message )
  2.  
  3. # check $VerbosePreference variable, and turns -Verbose on
  4. { if ( $VerbosePreference -ne 'SilentlyContinue' )
  5. { Write-Host " $Message" -ForegroundColor 'Yellow' } }
  6.  
  7. $VerbosePreference = "Continue"
  8. $LogDate = get-date -format "MM-d-yy-HH"
  9. $objShell = New-Object -ComObject Shell.Application
  10. $binFolder = $objShell.Namespace(0xA)
  11. $ErrorActionPreference = "SilentlyContinue"
  12.                    
  13. Start-Transcript -Path C:\Windows\Temp\$LogDate.log
  14.  
  15. ## Cleans all code off of the screen.
  16. Clear-Host
  17.  
  18. $size = Get-ChildItem C:\Users\* -Include *.iso, *.vhd -Recurse -ErrorAction SilentlyContinue |
  19. Sort Length -Descending |
  20. Select-Object Name, Directory,
  21. @{Name="Size (GB)";Expression={ "{0:N2}" -f ($_.Length / 1GB) }} |
  22. Format-Table -AutoSize | Out-String
  23.  
  24. $Before = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
  25. @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
  26. @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
  27. @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
  28. @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } |
  29. Format-Table -AutoSize | Out-String                      
  30.                    
  31. ## Stops the windows update service.
  32. Get-Service -Name wuauserv | Stop-Service -Force -Verbose -ErrorAction SilentlyContinue
  33. ## Windows Update Service has been stopped successfully!
  34.  
  35. $DaysToDelete = 1
  36.  
  37. $distribDir = "C:\Windows\SoftwareDistribution\*"  ## Deletes the contents of windows software distribution.
  38. $windowsTempDir = "C:\Windows\Temp\*" ## Deletes the contents of the Windows Temp folder.
  39. $tempDir = "C:\Temp\*" ## Deletes all cookies.
  40. #$locSetDir = "C:\Documents and Settings\*\Local Settings\Temp\*"  ## Delets all local settings temp
  41. #$locSetIEDir = "C:\Documents and Settings\*\Local Settings\Temporary Internet Files\*"   ## Delets all local settings IE temp
  42. #$locSetHisDir = "C:\Documents and Settings\*\Local Settings\History\*"  ## Delets all local settings history
  43.  
  44. Get-ChildItem $distribDir, $windowsTempDir $tempDir -Recurse -Force -Verbose -ErrorAction SilentlyContinue |
  45. Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete)) } |
  46. remove-item -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
  47.  
  48. $targets = "AppData\Local\Temp",
  49.            "AppData\Local\Microsoft\Windows\Temporary Internet Files",
  50.            "AppData\Local\Microsoft\Windows\INetCache\",
  51.            "AppData\Local\Microsoft\Windows\Caches"
  52.  
  53. $special = @("SSHD", "Public", "Default User")
  54.  
  55. $profiles = Get-ChildItem "C:\Users" -Directory -Force |
  56.     Where-Object Name -NotIn $special |
  57.     Select-Object -ExpandProperty FullName
  58.  
  59. $profiles | ForEach-Object {
  60.     foreach($target in $targets) {
  61.         $path = Join-Path $_ $target
  62.         remove-item $path -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue
  63.     }
  64. }
  65.  
  66. $DaysToDelete = 2
  67.  
  68. $targets = "profile/Default",
  69.            "profile/Profile 1",
  70.            "profile/Profile 2",
  71.            "profile/Profile 3",
  72.            "profile/Profile 4"
  73.  
  74. $special = @("chromium", "64")
  75.  
  76. $profiles = Get-ChildItem "C:\" -Directory -Force |
  77.     Where-Object Name -In $special |
  78.     Select-Object -ExpandProperty FullName
  79.    
  80. $chromeDir = "C:\Users\*\AppData\Local\Google\Chrome\User Data\Default"
  81. $chromeSetDir = "C:\Users\*\Local Settings\Application Data\Google\Chrome\User Data\Default"
  82.  
  83. $Items = @("*Archived History*",
  84.             "*Cache*",
  85.             "*Cookies*",
  86.             "*History*",
  87.             "*Top Sites*",
  88.             "*Visited Links*",
  89.             "*Web Data*")
  90.  
  91. $profiles | ForEach-Object {
  92.     foreach($target in $targets) {
  93.         $profile = Join-Path $_ $target
  94.        
  95.         $items | ForEach-Object {
  96.         $item = $_
  97.         Get-ChildItem $profile, $chromeDir, $chromeSetDir -Recurse -Force -ErrorAction SilentlyContinue |
  98.             Where-Object { ($_.CreationTime -lt $(Get-Date).AddDays(-$DaysToDelete))  -and $_.Directory -like $item} | ForEach-Object {
  99.             $path = Join-Path $_.DirectoryName $_
  100.             Remove-Item $path -force -Verbose -recurse -ErrorVariable FailedItems -ErrorAction SilentlyContinue }
  101.          }
  102.          
  103.     }
  104. }
  105.            
  106.  
  107.  
  108. ## deletes the contents of the recycling Bin.
  109. $binFolder.items() | ForEach-Object { Remove-Item $_.path -ErrorAction Ignore -Force -Verbose -Recurse }
  110. ## The Recycling Bin has been emptied!
  111.  
  112. #$cleanmgr = get-childitem cleanmgr.exe | select -expandproperty lastaccesstime
  113. If ((Get-Date).Day%7 -eq 0) {
  114. ## Running Disk Clean up Tool
  115.     write-Host "Finally now, Running Windows disk Clean up Tool" -ForegroundColor Cyan
  116.     cleanmgr /sagerun:11 | out-Null
  117.    
  118.     $([char]7)
  119.     Sleep 1
  120.     $([char]7)
  121.     Sleep 1    
  122. }
  123.  
  124. ## Starts the Windows Update Service
  125. ##Get-Service -Name wuauserv | Start-Service -Verbose
  126.  
  127. $After =  Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq "3" } | Select-Object SystemName,
  128. @{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
  129. @{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f( $_.Size / 1gb)}},
  130. @{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f( $_.Freespace / 1gb ) } },
  131. @{ Name = "PercentFree" ; Expression = {"{0:P1}" -f( $_.FreeSpace / $_.Size ) } } |
  132. Format-Table -AutoSize | Out-String
  133.  
  134. ## Sends some before and after info for ticketing purposes
  135. Write-Verbose "Before: $Before"
  136. Write-Verbose "After: $After"
  137. Write-Verbose $size
  138. Write-Verbose "Items failed to delete:"
  139. Write-Verbose $FailedItems | Foreach-Object {$_.CategoryInfo.TargetName}
  140.  
  141. "Disk C distribution:"
  142. Get-ChildItem C:\* -Directory |
  143. Foreach {
  144.   $stat = dir $_.FullName -File -Recurse |
  145.      measure Length -Sum
  146.   $_ | Add-Member -MemberType NoteProperty `
  147.      -Name Files -Value $stat.count
  148.   $_ | Add-Member -MemberType NoteProperty `
  149.     -Name "Size (Gb)" -Value ("{0:N1}" -f( $stat.sum / 1gb )) -PassThru
  150. } | select -Last 10 | Sort FileSize | Select Name ,"Size (Gb)",Files |
  151. Format-Table -AutoSize | Out-String
  152.  
  153. ## Completed Successfully!
  154. Get-Date | Select-Object DateTime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement