-
Notifications
You must be signed in to change notification settings - Fork 2
/
Invoke-CleanManager.ps1
160 lines (148 loc) · 6.18 KB
/
Invoke-CleanManager.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<#
.SYNOPSIS
Runs the Windows Disk Cleanup tool with predefined options.
If you want to enable- or disable some options, you can comment or uncomment lines in $Sections.
.DESCRIPTION
Default enabled options are:
'Active Setup Temp Folders',
'BranchCache',
'Device Driver Packages',
'Downloaded Program Files',
'GameNewsFiles',
'GameStatisticsFiles',
'GameUpdateFiles',
'Memory Dump Files',
'Offline Pages Files',
'Old ChkDsk Files',
'Previous Installations',
'Service Pack Cleanup',
'Setup Log Files',
'System error memory dump files',
'System error minidump files',
'Temporary Files',
'Temporary Setup Files',
'Thumbnail Cache',
'Update Cleanup',
'Upgrade Discarded Files',
'Windows Defender',
'Windows ESD installation files',
'Windows Error Reporting Archive Files'
'Windows Error Reporting Queue Files',
'Windows Error Reporting System Archive Files',
'Windows Error Reporting System Queue Files',
'Windows Upgrade Log Files'
Default disabled options:
"User file versions"
'Recycle Bin',
'Temporary Sync Files',
"DownloadsFolder",
.NOTES
Author: Tom de Leeuw
Website: https://tech-tom.com / https://ucsystems.nl
#>
function Invoke-CleanManager {
begin {
# Get disk space for comparison afterwards
$Before = Get-DiskSpace
# Checks if Disk Cleanup is installed, if not; install it (For Server 2008 R2 without Cleanmgr preinstalled)
if ( ! (Test-Path "$env:SystemRoot\System32\cleanmgr.exe")) {
Write-Warning 'Windows Cleanup NOT installed! Trying installation...'
try {
if (Test-Path "$env:SystemRoot\Winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe") {
Copy-Item "$env:SystemRoot\Winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe" -Destination "$env:SystemRoot\System32"
}
if (Test-Path "$env:SystemRoot\Winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63\cleanmgr.exe.mui") {
Copy-Item "$env:SystemRoot\Winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63\cleanmgr.exe.mui" -Destination "$env:SystemRoot\System32\en-US"
}
elseif ( ! (Get-WindowsFeature 'Desktop-Experience')) {
Write-Verbose 'Manual file copy failed. Installing Desktop Experience feature...'
Install-WindowsFeature 'Desktop-Experience'
}
}
catch {
Write-Error $_
}
}
# Check if Disk Cleanup is available again before continuing
if ( ! (Test-Path "$env:SystemRoot\System32\cleanmgr.exe")) {
throw "Windows Disk Cleanup tool not installed! Aborting..."
}
# Enabled sections to be used by Disk Cleanup
$Sections = @(
'Active Setup Temp Folders',
'BranchCache',
'Device Driver Packages',
'Downloaded Program Files',
#"DownloadsFolder",
'GameNewsFiles',
'GameStatisticsFiles',
'GameUpdateFiles',
#'Memory Dump Files',
'Offline Pages Files',
'Old ChkDsk Files',
'Previous Installations',
#'Recycle Bin',
'Service Pack Cleanup',
'Setup Log Files',
#'System error memory dump files',
#'System error minidump files',
'Temporary Files',
'Temporary Setup Files',
#'Temporary Sync Files',
'Thumbnail Cache',
'Update Cleanup',
'Upgrade Discarded Files',
#"User file versions"
'Windows Defender',
'Windows ESD installation files',
'Windows Error Reporting Archive Files'
'Windows Error Reporting Queue Files',
'Windows Error Reporting System Archive Files',
'Windows Error Reporting System Queue Files',
'Windows Upgrade Log Files'
)
}
process {
# Clear current registry entries
Write-Verbose 'Clearing current CleanMgr.exe automation settings...'
$RegistryParams = @{
Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*'
Name = 'StateFlags0001'
ErrorAction = 'SilentlyContinue'
}
[void] (Get-ItemProperty @RegistryParams | Remove-ItemProperty -Name 'StateFlags0001' -ErrorAction SilentlyContinue)
# Add registry entries according to $Sections defined above
Write-Verbose 'Adding enabled disk cleanup sections'
foreach ($key in $Sections) {
$newItemParams = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$key"
Name = 'StateFlags0001'
Value = 1
PropertyType = 'DWord'
ErrorAction = 'SilentlyContinue'
}
[void] (New-ItemProperty @newItemParams)
}
try {
Write-Verbose 'Running CleanMgr.exe...'
Start-Process -FilePath "$env:systemroot\system32\Cleanmgr.exe" -ArgumentList '/sagerun:1' -NoNewWindow -Wait
Write-Verbose 'Waiting for CleanMgr and DismHost processes.'
Get-Process -Name cleanmgr, dismhost -ErrorAction SilentlyContinue | Wait-Process
}
catch {
Write-Error $_
}
}
end {
# Get disk space again and calculate difference
$After = Get-DiskSpace
$TotalCleaned = "$(($After.FreeSpace - $Before.FreeSpace).ToString('00.00')) GB"
# Report
if ($null -ne $script:CleanupReport) {
$script:CleanupReport.CleanManager = $TotalCleaned
}
else {
Write-Output "Total space cleaned: $TotalCleaned"
}
}
}