→ 🚀What's New? Explore Lansweeper's Fall 2024 Updates! Fall Launch Blog !
‎06-09-2020 07:09 PM
‎06-10-2020 03:52 AM
[CmdletBinding(SupportsShouldProcess=$true)]
Param()
##Run CleanUpMGR Automatically
Write-Verbose 'Beging script CleanUpMGRScript.ps1 target C:'
#Capture current free disk space on Drive C
$FreeSpaceBefore = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB
$Computer = $env:COMPUTERNAME
$RegPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches'
$RegName = 'StateFlags0100'
$RegValue = '3'
Write-Verbose 'Setting values for disk cleanup.'
New-ItemProperty -Path "$Regpath\Active Setup Temp Folders" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Downloaded Program Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Internet Cache Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Memory Dump Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Offline Page Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Old ChkDsk Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Previous Installations" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Recycle Bin" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Service Pack Cleanup" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Setup Log Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\System Error Memory Dump Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\System Error Minidump Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Temporary Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Temporary Setup Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Thumbnail Cache" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Update Cleanup" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Upgrade Discarded Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Defender" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Error Reporting Archive Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Error Reporting Queue Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Error Reporting System Archive Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Error Reporting System Queue Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows ESD Installation Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
New-ItemProperty -Path "$Regpath\Windows Upgrade Log Files" -Name $RegName -Value $RegValue -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
Write-Verbose 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:100' -WindowStyle Hidden -Wait
Write-Verbose 'Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.'
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process
$UpdateCleanupSuccessful = $false
if (Test-Path $env:SystemRoot\Logs\CBS\DeepClean.log) {
$UpdateCleanupSuccessful = Select-String -Path $env:SystemRoot\Logs\CBS\DeepClean.log -Pattern 'Total size of superseded packages:' -Quiet
Write-Verbose "Update Cleanup Successful: $($UpdateCleanupSuccessful)"
}
$FreespaceAfter = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB
Write-Verbose "$($Computer), $(Get-Date), Free Space Before: $($FreeSpaceBefore), Free Space After: $($FreeSpaceAfter), Reboot required: $($UpdateCleanupSuccessful)"
Write-Output "$($Computer), $(Get-Date), Free Space Before: $($FreeSpaceBefore), Free Space After: $($FreeSpaceAfter), Reboot required: $($UpdateCleanupSuccessful)" | Out-File "C:\Windows\Temp\DiskSpace.log" -Append
if ($UpdateCleanupSuccessful) {
Write-Verbose 'Rebooting to complete CleanMgr.exe Update Cleanup....'
#SHUTDOWN.EXE /r /f /t 0 /c 'Rebooting to complete CleanMgr.exe Update Cleanup....'
Exit 3010
}
Experience Lansweeper with your own data. Sign up now for a 14-day free trial.
Try Now