├── .gitignore ├── README.md └── Clear-TempFiles.ps1 /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .vscode/settings.json 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Clean Browser Cache and Recycle Bin 2 | 3 | This Powershell script was created by [Lemtek](https://github.com/lemtek/Powershell/blob/master/Clear_Browser_Caches) and has been edited with changes and additions by [Bromeego](https://github.com/Bromeego/Clean-Temp-Files) and from other users which have forked earlier versions. Credit and thanks is noted below in the changelog. 4 | 5 | Powershell script to delete cache & cookies in Firefox, Chrome, Chromium, Opera, Yandex, Edge & IE browsers. With options to empty the Recycle Bin for all users and Downloads folder for files older than 90 days. 6 | 7 | v2.8.2: 8 | 9 | * Added cleaning of Windows Error Reporting and CBS (Component-Based Servicing) folders 10 | 11 | v2.8.1: 12 | 13 | * Added cleaning of Inetpub logfiles directory 14 | * Added cleaning of user CrashDumps directory 15 | 16 | v2.8: 17 | 18 | * Added cleaning of Microsoft Teams previous version folder 19 | * Added Dropbox cache cleaning - Found on [bluPhy](https://github.com/bluPhy/Clean-Temp-Files) - Thanks! 20 | * Added SnagIt CrashDump cleaning 21 | * Added Yandex Browser 22 | * Added another Cache folder for Internet Explorer/Edge 23 | * Added clearing of Firefox OfflineCache folder 24 | * Added deleting of files older than 90 days within User\Downloads Folder. The date can be changed on line 28 25 | * Removed unneeded command from Firefox cleaning 26 | * Fixed command for Firefox cleaning 27 | * Split Internet Explorer, User Temp Folders, Opera and Chromium to their own sections 28 | * Split Opera and Chromium sections into their own 29 | * Renamed Internet Explorer section to Internet Explorer & Edge 30 | * Expanded the -EA parameter to read the full name 31 | * Fixed output error on line 37 - Found on [bluPhy](https://github.com/bluPhy/Clean-Temp-Files) - Thanks! 32 | * Updated README.md with proper formatting 33 | 34 | v2.7: 35 | 36 | * Borrowed Chromium and Opera Cleaning - Credit [Anst-foto](https://github.com/anst-foto/Powershell) 37 | * Redone Recycle Bin cleaning. Will ask for confirmation at the start of the script then will clean All Users Recycle Bin - Credit [Chris Rakowitz](https://community.spiceworks.com/scripts/show_download/3677-empty-recycle-bins) 38 | * Translate SID to User account when running the Recycle Bin Cleaning for nicer output. If SID cannot be translated then just show SID 39 | 40 | v2.6: 41 | 42 | * Fixes from Github which were not pulled from Master 43 | * Fixed C:\users\\%username% could not be found if the profiledir points to another directory - Credit [Mahagon](https://github.com/Mahagon/Powershell) 44 | * Amend Clear Internet Explorer Output - Credit [Watnabe](https://github.com/Watnabe/Powershell) 45 | 46 | v2.5: 47 | 48 | * Added Disk Size, Free Space, % Free. Before and After - Code Borrowed from [Technet Article](https://gallery.technet.microsoft.com/scriptcenter/Clean-up-your-C-Drive-bc7bb3ed) 49 | * Write to Text File 50 | * Tabbed in code, cleaner to read 51 | * Updated Alias' to Full Content for easier maintenance 52 | 53 | v2.4: 54 | 55 | * Resolved *.default issue, issue was with the file path name not with *.default, but issue resolved 56 | 57 | v2.3: 58 | 59 | * Added Cache2 to Mozilla directories but found that *.default is not working 60 | 61 | v2.2: 62 | 63 | * Added Cyan colour to verbose output 64 | 65 | v2.1: 66 | 67 | * Added the location 'C:\Windows\Temp\*' and 'C:\`$recycle.bin\' 68 | 69 | v2: 70 | 71 | * Changed the retrieval of user list to dir the c:\users folder and export to csv 72 | 73 | v1: 74 | 75 | * Compiled script 76 | -------------------------------------------------------------------------------- /Clear-TempFiles.ps1: -------------------------------------------------------------------------------- 1 | #Calling Powershell as Admin and setting Execution Policy to Bypass to avoid Cannot run Scripts error 2 | param ([switch]$Elevated) 3 | function CheckAdmin { 4 | $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) 5 | $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) 6 | } 7 | if ((CheckAdmin) -eq $false) { 8 | if ($elevated) { 9 | # could not elevate, quit 10 | } 11 | else { 12 | # Detecting Powershell (powershell.exe) or Powershell Core (pwsh), will return true if Powershell Core (pwsh) 13 | if ($IsCoreCLR) { $PowerShellCmdLine = 'pwsh.exe' } else { $PowerShellCmdLine = 'powershell.exe' } 14 | $CommandLine = "-noprofile -ExecutionPolicy Bypass -File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments + ' -Elevated' 15 | Start-Process "$PSHOME\$PowerShellCmdLine" -Verb RunAs -ArgumentList $CommandLine 16 | } 17 | exit 18 | } 19 | 20 | # Rename Title Window 21 | $host.ui.RawUI.WindowTitle = 'Clean Browser Temp Files' 22 | 23 | function Cleanup { 24 | # Set Date for Log 25 | $LogDate = Get-Date -Format 'MM-d-yy-HHmm' 26 | 27 | # Ask for confirmation to delete users Downloaded files - Anything older than 90 days 28 | $DeleteOldDownloads = Read-Host 'Would you like to delete files older than 90 days in the Downloads folder for All Users? (Y/N)' 29 | 30 | # Set Deletion Date for Downloads Folder 31 | $DelDownloadsDate = (Get-Date).AddDays(-90) 32 | 33 | # Set Deletion Date for Inetpub Log Folder 34 | $DelInetLogDate = (Get-Date).AddDays(-30) 35 | 36 | # Set Deletion Date for System32 Log Folder 37 | $System32LogDate = (Get-Date).AddMonths(-2) 38 | 39 | # Set Deletion Date for Azure Logs Folder 40 | $DelAZLogDate = (Get-Date).AddDays(-7) 41 | 42 | # Set Deletion Date for Office File Cache Folder 43 | $DelOfficeCacheDate = (Get-Date).AddDays(-7) 44 | 45 | # Set Deletion Date for LFSAgent Logs Folder 46 | $DelLFSAGentLogDate = (Get-Date).AddDays(-30) 47 | 48 | # Set Deletion Date for SotiMobicontroller Logs 49 | $DelSotiLogDate = (Get-Date).AddYears(-1) 50 | 51 | # Ask for Confirmation to Empty Recycle Bin for All Users 52 | $CleanBin = Read-Host 'Would you like to empty the Recycle Bin for All Users? (Y/N)' 53 | 54 | # Get the size of the Windows Updates folder (SoftwareDistribution) 55 | $WUfoldersize = (Get-ChildItem "$env:windir\SoftwareDistribution" -Recurse | Measure-Object Length -s).sum / 1Gb 56 | 57 | # Ask the user if they would like to clean the Windows Update folder 58 | if ($WUfoldersize -gt 1.5) { 59 | Write-Host 'The Windows Update folder is' ('{0:N2} GB' -f $WUFoldersize) 60 | $CleanWU = Read-Host 'Do you want clean the Software Distribution folder and reset Windows Updates? (Y/N)' 61 | } 62 | 63 | # Get Disk Size 64 | $Before = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq '3' } | Select-Object SystemName, 65 | @{ Name = 'Drive' ; Expression = { ( $_.DeviceID ) } }, 66 | @{ Name = 'Size (GB)' ; Expression = { '{0:N1}' -f ( $_.Size / 1gb) } }, 67 | @{ Name = 'FreeSpace (GB)' ; Expression = { '{0:N1}' -f ( $_.Freespace / 1gb ) } }, 68 | @{ Name = 'PercentFree' ; Expression = { '{0:P1}' -f ( $_.FreeSpace / $_.Size ) } } | 69 | Format-Table -AutoSize | Out-String 70 | 71 | # Define log file location 72 | $Cleanuplog = "$env:USERPROFILE\Cleanup$LogDate.log" 73 | 74 | # Start Logging 75 | Start-Transcript -Path "$CleanupLog" 76 | 77 | # Create list of users 78 | Write-Host -ForegroundColor Green "Getting the list of Users`n" 79 | $Users = Get-ChildItem 'C:\Users' | Select-Object Name 80 | $users = $Users.Name 81 | 82 | # Begin! 83 | Write-Host -ForegroundColor Green "Beginning Script...`n" 84 | 85 | # Clear Firefox Cache 86 | Write-Host -ForegroundColor Green "Clearing Firefox Cache`n" 87 | foreach ($user in $Users) { 88 | if (Test-Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles") { 89 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 90 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 91 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\thumbnails\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 92 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\cookies.sqlite" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 93 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\webappsstore.sqlite" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 94 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\chromeappsstore.sqlite" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 95 | Remove-Item -Path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*\OfflineCache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 96 | } 97 | Write-Host -ForegroundColor Yellow "Done...`n" 98 | } 99 | # Clear Google Chrome 100 | Write-Host -ForegroundColor Green "Clearing Google Chrome Cache`n" 101 | foreach ($user in $Users) { 102 | if (Test-Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data") { 103 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 104 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 105 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cookies" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 106 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Media Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 107 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 108 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\JumpListIconsOld" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 109 | # Comment out the following line to remove the Chrome Write Font Cache too. 110 | # Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\ChromeDWriteFontCache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 111 | 112 | # Check Chrome Profiles. It looks as though when creating profiles, it just numbers them Profile 1, Profile 2 etc. 113 | $Profiles = Get-ChildItem -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data" | Select-Object Name | Where-Object Name -Like 'Profile*' 114 | foreach ($Account in $Profiles) { 115 | $Account = $Account.Name 116 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 117 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\Cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 118 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\Cookies" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 119 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\Media Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 120 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\Cookies-Journal" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 121 | Remove-Item -Path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\$Account\JumpListIconsOld" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 122 | } 123 | } 124 | Write-Host -ForegroundColor Yellow "Done...`n" 125 | } 126 | 127 | # Clear Internet Explorer & Edge 128 | Write-Host -ForegroundColor Yellow "Clearing Internet Explorer & Old Edge Cache`n" 129 | foreach ($user in $Users) { 130 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 131 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Windows\INetCache\* " -Recurse -Force -ErrorAction SilentlyContinue -Verbose 132 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Windows\WebCache\* " -Recurse -Force -ErrorAction SilentlyContinue -Verbose 133 | } 134 | Write-Host -ForegroundColor Yellow "Done...`n" 135 | 136 | # Clear Edge Chromium 137 | Write-Host -ForegroundColor Yellow "Clearing Edge Chromium Cache`n" 138 | taskkill /F /IM msedge.exe 139 | foreach ($user in $Users) { 140 | if (Test-Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data") { 141 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 142 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 143 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Cookies" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 144 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Media Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 145 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Cookies-Journal" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 146 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\JumpListIconsOld" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 147 | # Comment out the following line to remove the Edge Write Font Cache too. 148 | # Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\EdgeDWriteFontCache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 149 | 150 | # Check Edge Profiles. It looks as though when creating profiles, it just numbers them Profile 1, Profile 2 etc. 151 | $Profiles = Get-ChildItem -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data" | Select-Object Name | Where-Object Name -Like 'Profile*' 152 | foreach ($Account in $Profiles) { 153 | $Account = $Account.Name 154 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 155 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\Cache2\entries\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 156 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\Cookies" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 157 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\Media Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 158 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\Cookies-Journal" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 159 | #Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\$Account\JumpListIconsOld" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 160 | } 161 | } 162 | Write-Host -ForegroundColor Yellow "Done...`n" 163 | } 164 | 165 | # Clear Chromium 166 | Write-Host -ForegroundColor Yellow "Clearing Chromium Cache`n" 167 | foreach ($user in $Users) { 168 | if (Test-Path "C:\Users\$user\AppData\Local\Chromium") { 169 | Remove-Item -Path "C:\Users\$user\AppData\Local\Chromium\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 170 | Remove-Item -Path "C:\Users\$user\AppData\Local\Chromium\User Data\Default\GPUCache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 171 | Remove-Item -Path "C:\Users\$user\AppData\Local\Chromium\User Data\Default\Media Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 172 | Remove-Item -Path "C:\Users\$user\AppData\Local\Chromium\User Data\Default\Pepper Data" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 173 | Remove-Item -Path "C:\Users\$user\AppData\Local\Chromium\User Data\Default\Application Cache" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 174 | } 175 | Write-Host -ForegroundColor Yellow "Done...`n" 176 | } 177 | 178 | # Clear Opera 179 | Write-Host -ForegroundColor Yellow "Clearing Opera Cache`n" 180 | foreach ($user in $Users) { 181 | if (Test-Path "C:\Users\$user\AppData\Local\Opera Software") { 182 | Remove-Item -Path "C:\Users\$user\AppData\Local\Opera Software\Opera Stable\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 183 | } 184 | Write-Host -ForegroundColor Yellow "Done...`n" 185 | } 186 | 187 | # Clear Yandex 188 | Write-Host -ForegroundColor Yellow "Clearing Yandex Cache`n" 189 | foreach ($user in $Users) { 190 | if (Test-Path "C:\Users\$user\AppData\Local\Yandex") { 191 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\User Data\Default\Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 192 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\User Data\Default\GPUCache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 193 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\User Data\Default\Media Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 194 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\User Data\Default\Pepper Data\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 195 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\User Data\Default\Application Cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 196 | Remove-Item -Path "C:\Users\$user\AppData\Local\Yandex\YandexBrowser\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 197 | } 198 | Write-Host -ForegroundColor Yellow "Done...`n" 199 | } 200 | 201 | # Clear User Temp Folders 202 | Write-Host -ForegroundColor Yellow "Clearing User Temp Folders`n" 203 | foreach ($user in $Users) { 204 | Remove-Item -Path "C:\Users\$user\AppData\Local\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 205 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Windows\WER\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 206 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Windows\AppCache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 207 | Remove-Item -Path "C:\Users\$user\AppData\Local\CrashDumps\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 208 | } 209 | Write-Host -ForegroundColor Yellow "Done...`n" 210 | # Clear Windows Temp Folder 211 | Write-Host -ForegroundColor Yellow "Clearing Windows Temp Folder`n" 212 | foreach ($user in $Users) { 213 | Remove-Item -Path 'C:\Temp\*' -Recurse -Force -ErrorAction SilentlyContinue -Verbose 214 | Remove-Item -Path "$env:windir\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 215 | Remove-Item -Path "$env:windir\Logs\CBS\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 216 | Remove-Item -Path "$env:ProgramData\Microsoft\Windows\WER\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 217 | # Only grab log files sitting in the root of the Logfiles directory 218 | $Sys32Files = Get-ChildItem -Path "$env:windir\System32\LogFiles" | Where-Object { ($_.name -like '*.log') -and ($_.lastwritetime -lt $System32LogDate) } 219 | foreach ($File in $Sys32Files) { 220 | Remove-Item -Path "$env:windir\System32\LogFiles\$($file.name)" -Force -ErrorAction SilentlyContinue -Verbose 221 | } 222 | } 223 | Write-Host -ForegroundColor Yellow "Done...`n" 224 | 225 | # Clear Inetpub Logs Folder 226 | if (Test-Path 'C:\inetpub\logs\LogFiles\') { 227 | Write-Host -ForegroundColor Yellow "Clearing Inetpub Logs Folder`n" 228 | $Folders = Get-ChildItem -Path 'C:\inetpub\logs\LogFiles\' | Select-Object Name 229 | foreach ($Folder in $Folders) { 230 | $folder = $Folder.Name 231 | Remove-Item -Path "C:\inetpub\logs\LogFiles\$Folder\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose | Where-Object LastWriteTime -LT $DelInetLogDate 232 | } 233 | Write-Host -ForegroundColor Yellow "Done...`n" 234 | } 235 | 236 | # Delete Microsoft Teams Previous Version files 237 | Write-Host -ForegroundColor Yellow "Clearing Teams Previous version`n" 238 | foreach ($user in $Users) { 239 | if (Test-Path "C:\Users\$user\AppData\Local\Microsoft\Teams\") { 240 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Teams\previous\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 241 | Remove-Item -Path "C:\Users\$user\AppData\Local\Microsoft\Teams\stage\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 242 | } 243 | } 244 | Write-Host -ForegroundColor Yellow "Done...`n" 245 | 246 | # Delete SnagIt Crash Dump files 247 | Write-Host -ForegroundColor Yellow "Clearing SnagIt Crash Dumps`n" 248 | foreach ($user in $Users) { 249 | if (Test-Path "C:\Users\$user\AppData\Local\TechSmith\SnagIt") { 250 | Remove-Item -Path "C:\Users\$user\AppData\Local\TechSmith\SnagIt\CrashDumps\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 251 | } 252 | } 253 | Write-Host -ForegroundColor Yellow "Done...`n" 254 | 255 | # Clear Dropbox 256 | Write-Host -ForegroundColor Yellow "Clearing Dropbox Cache`n" 257 | foreach ($user in $Users) { 258 | if (Test-Path "C:\Users\$user\Dropbox\") { 259 | Remove-Item -Path "C:\Users\$user\Dropbox\.dropbox.cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 260 | Remove-Item -Path "C:\Users\$user\Dropbox*\.dropbox.cache\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 261 | } 262 | } 263 | Write-Host -ForegroundColor Yellow "Done...`n" 264 | 265 | # Clear HP Support Assistant Installation Folder 266 | if (Test-Path 'C:\swsetup') { 267 | Remove-Item -Path 'C:\swsetup' -Force -ErrorAction SilentlyContinue -Verbose 268 | } 269 | 270 | # Delete files older than 90 days from Downloads folder 271 | if ($DeleteOldDownloads -eq 'Y') { 272 | Write-Host -ForegroundColor Yellow "Deleting files older than 90 days from User Downloads folder`n" 273 | foreach ($user in $Users) { 274 | $UserDownloads = "C:\Users\$user\Downloads" 275 | $OldFiles = Get-ChildItem -Path "$UserDownloads\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object LastWriteTime -LT $DelDownloadsDate 276 | foreach ($file in $OldFiles) { 277 | Remove-Item -Path "$UserDownloads\$file" -Force -ErrorAction SilentlyContinue -Verbose 278 | } 279 | } 280 | Write-Host -ForegroundColor Yellow "Done...`n" 281 | } 282 | 283 | # Delete files older than 7 days from Azure Log folder 284 | if (Test-Path 'C:\WindowsAzure\Logs') { 285 | Write-Host -ForegroundColor Yellow "Deleting files older than 7 days from Azure Log folder`n" 286 | $AzureLogs = 'C:\WindowsAzure\Logs' 287 | $OldFiles = Get-ChildItem -Path "$AzureLogs\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object LastWriteTime -LT $DelAZLogDate 288 | foreach ($file in $OldFiles) { 289 | Remove-Item -Path "$AzureLogs\$file" -Force -ErrorAction SilentlyContinue -Verbose 290 | } 291 | Write-Host -ForegroundColor Yellow "Done...`n" 292 | } 293 | 294 | # Delete files older than 7 days from Office Cache Folder 295 | Write-Host -ForegroundColor Yellow "Clearing Office Cache Folder`n" 296 | foreach ($user in $Users) { 297 | $officecache = "C:\Users\$user\AppData\Local\Microsoft\Office\16.0\GrooveFileCache" 298 | if (Test-Path $officecache) { 299 | $OldFiles = Get-ChildItem -Path "$officecache\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object LastWriteTime -LT $DelOfficeCacheDate 300 | foreach ($file in $OldFiles) { 301 | Remove-Item -Path "$officecache\$file" -Force -ErrorAction SilentlyContinue -Verbose 302 | } 303 | } 304 | } 305 | Write-Host -ForegroundColor Yellow "Done...`n" 306 | 307 | # Delete files older than 30 days from LFSAgent Log folder https://www.lepide.com/ 308 | if (Test-Path "$env:windir\LFSAgent\Logs") { 309 | Write-Host -ForegroundColor Yellow "Deleting files older than 30 days from LFSAgent Log folder`n" 310 | $LFSAgentLogs = "$env:windir\LFSAgent\Logs" 311 | $OldFiles = Get-ChildItem -Path "$LFSAgentLogs\" -Recurse -File -ErrorAction SilentlyContinue | Where-Object LastWriteTime -LT $DelLFSAGentLogDate 312 | foreach ($file in $OldFiles) { 313 | Remove-Item -Path "$LFSAgentLogs\$file" -Force -ErrorAction SilentlyContinue -Verbose 314 | } 315 | Write-Host -ForegroundColor Yellow "Done...`n" 316 | } 317 | 318 | # Delete SOTI MobiController Log files older than 1 year 319 | if (Test-Path 'C:\Program Files (x86)\SOTI\MobiControl') { 320 | Write-Host -ForegroundColor Yellow "Deleting SOTI MobiController Log files older than 1 year`n" 321 | $SotiLogFiles = Get-ChildItem -Path 'C:\Program Files (x86)\SOTI\MobiControl' | Where-Object { ($_.name -like '*Device*.log' -or $_.name -like '*Server*.log' ) -and ($_.lastwritetime -lt $DelSotiLogDate) } 322 | foreach ($File in $SotiLogFiles) { 323 | Remove-Item -Path "C:\Program Files (x86)\SOTI\MobiControl\$($file.name)" -Force -ErrorAction SilentlyContinue -Verbose 324 | } 325 | Write-Host -ForegroundColor Yellow "Done...`n" 326 | } 327 | 328 | # Delete old Cylance Log files 329 | if (Test-Path 'C:\Program Files\Cylance\Desktop') { 330 | Write-Host -ForegroundColor Yellow "Deleting Old Cylance Log files`n" 331 | $OldCylanceLogFiles = Get-ChildItem -Path 'C:\Program Files\Cylance\Desktop' | Where-Object name -Like 'cylog-*.log' 332 | foreach ($File in $OldCylanceLogFiles) { 333 | Remove-Item -Path "C:\Program Files\Cylance\Desktop\$($file.name)" -Force -ErrorAction SilentlyContinue -Verbose 334 | } 335 | Write-Host -ForegroundColor Yellow "Done...`n" 336 | } 337 | 338 | # Delete Windows Updates Folder (SoftwareDistribution) and reset the Windows Update Service 339 | if ($CleanWU -eq 'Y') { 340 | Write-Host -ForegroundColor Yellow "Restarting Windows Update Service and Deleting SoftwareDistribution Folder`n" 341 | # Stop the Windows Update service 342 | try { 343 | Stop-Service -Name wuauserv 344 | } 345 | catch { 346 | $ErrorMessage = $_.Exception.Message 347 | Write-Warning "$ErrorMessage" 348 | } 349 | # Delete the folder 350 | Remove-Item "$env:windir\SoftwareDistribution" -Recurse -Force -ErrorAction SilentlyContinue -Verbose 351 | Start-Sleep -s 3 352 | 353 | # Start the Windows Update service 354 | try { 355 | Start-Service -Name wuauserv 356 | } 357 | catch { 358 | $ErrorMessage = $_.Exception.Message 359 | Write-Warning "$ErrorMessage" 360 | } 361 | Write-Host -ForegroundColor Yellow 'Done...' 362 | Write-Host -ForegroundColor Yellow "Please rerun Windows Update to pull down the latest updates `n" 363 | } 364 | 365 | # Empty Recycle Bin 366 | if ($Cleanbin -eq 'Y') { 367 | Write-Host -ForegroundColor Green "Cleaning Recycle Bin`n" 368 | $ErrorActionPreference = 'SilentlyContinue' 369 | $RecycleBin = "C:\`$Recycle.Bin" 370 | $BinFolders = Get-ChildItem $RecycleBin -Directory -Force 371 | 372 | foreach ($Folder in $BinFolders) { 373 | # Translate the SID to a User Account 374 | $objSID = New-Object System.Security.Principal.SecurityIdentifier ($folder) 375 | try { 376 | $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]) 377 | Write-Host -Foreground Yellow -Background Black "Cleaning $objUser Recycle Bin" 378 | } 379 | # If SID cannot be Translated, Throw out the SID instead of error 380 | catch { 381 | $objUser = $objSID.Value 382 | Write-Host -Foreground Yellow -Background Black "$objUser" 383 | } 384 | $Files = @() 385 | 386 | if ($PSVersionTable.PSVersion -like '*2*') { 387 | $Files = Get-ChildItem $Folder.FullName -Recurse -Force 388 | } 389 | else { 390 | $Files = Get-ChildItem $Folder.FullName -File -Recurse -Force 391 | $Files += Get-ChildItem $Folder.FullName -Directory -Recurse -Force 392 | } 393 | 394 | $FileTotal = $Files.Count 395 | 396 | for ($i = 1; $i -le $Files.Count; $i++) { 397 | $FileName = Select-Object -InputObject $Files[($i - 1)] 398 | Write-Progress -Activity 'Recycle Bin Clean-up' -Status "Attempting to Delete File [$i / $FileTotal]: $FileName" -PercentComplete (($i / $Files.count) * 100) -Id 1 399 | Remove-Item -Path $Files[($i - 1)].FullName -Recurse -Force 400 | } 401 | Write-Progress -Activity 'Recycle Bin Clean-up' -Status 'Complete' -Completed -Id 1 402 | } 403 | Write-Host -ForegroundColor Green "Done`n `n" 404 | } 405 | 406 | Write-Host -ForegroundColor Green "All Tasks Done!`n`n" 407 | 408 | 409 | # Get Drive size after clean 410 | $After = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq '3' } | Select-Object SystemName, 411 | @{ Name = 'Drive' ; Expression = { ( $_.DeviceID ) } }, 412 | @{ Name = 'Size (GB)' ; Expression = { '{0:N1}' -f ( $_.Size / 1gb) } }, 413 | @{ Name = 'FreeSpace (GB)' ; Expression = { '{0:N1}' -f ( $_.Freespace / 1gb ) } }, 414 | @{ Name = 'PercentFree' ; Expression = { '{0:P1}' -f ( $_.FreeSpace / $_.Size ) } } | 415 | Format-Table -AutoSize | Out-String 416 | 417 | # Sends some before and after info for ticketing purposes 418 | Write-Host -ForegroundColor Green "Before: $Before" 419 | Write-Host -ForegroundColor Green "After: $After" 420 | 421 | # Another reminder about running Windows update if needed as it would get lost in all the scrolling text. 422 | if ($CleanWU -eq 'Y') { 423 | Write-Host -ForegroundColor Yellow "`nPlease rerun Windows Update to pull down the latest updates. `n" 424 | } 425 | 426 | # Read some of the output before going away 427 | Start-Sleep -s 15 428 | 429 | # Completed Successfully! 430 | # Open Text File 431 | Invoke-Item $Cleanuplog 432 | 433 | # Stop Script 434 | Stop-Transcript 435 | } 436 | 437 | # Listing all files in C:\Temp\* recursively, using Force parameter displays hidden files. 438 | $TempItems = Get-ChildItem -Path 'C:\Temp\*' -Recurse -Force 439 | if ($TempItems.count -gt 1) { 440 | Write-Warning 'There are files within C:\Temp, please verify that important files are out of this location' 441 | $Cont = Read-Host 'Continue with the cleanup script [Y/N]' 442 | if ($cont -eq 'Y') { 443 | Cleanup 444 | } 445 | else { 446 | Write-Host 'Please check the files within C:\Temp before running the script again' 447 | Start-Sleep -Seconds 5 448 | } 449 | } 450 | else { 451 | Cleanup 452 | } 453 | --------------------------------------------------------------------------------