Repository navigation
Expand file tree
/
Copy pathSoftwareDistributionCleanup.ps1
More file actions
1696 lines (1399 loc) · 65.4 KB
/
Copy pathSoftwareDistributionCleanup.ps1
File metadata and controls
1696 lines (1399 loc) · 65.4 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Reclaim space from %SystemRoot%\SoftwareDistribution.
.DESCRIPTION
SoftwareDistribution is the Windows Update working area. The Download subfolder holds
update payloads. A payload that has already been installed is never reused, so it is
safe to clear once servicing is quiescent and nothing is downloaded and waiting to
install. The DataStore subfolder holds the update database.
This script clears Download by default. -Full additionally clears DataStore.
HOW IT WORKS
The default path is the rename-then-delete sequence Microsoft documents:
stop services -> rename Download to Download.<timestamp>.bak -> restart services
-> delete the .bak afterwards
Rename-Item is a metadata operation, so service downtime is milliseconds rather than
the minutes a recursive delete takes. It is also recoverable: the renamed folder sits
on disk until the delete finishes, and a .bak left behind by an interrupted run is
found, reported and deleted by the next run.
-OlderThanDays cannot use that sequence, because half a folder cannot be renamed. With
-OlderThanDays the script falls back to a selective in-place delete, which holds the
services down for the length of the delete.
SAFETY
- Dry run is the default. -Execute is required to change anything.
- Hard stops cannot be overridden by any switch:
* an update that is already downloaded and waiting to install, because its payload
is exactly what Download holds (asked of the Windows Update Agent, local cache
only, no network call)
* the servicing worker TiWorker.exe running, which means CBS is mid-transaction
* CBS RebootPending or RebootInProgress, WindowsUpdate RebootRequired, or
WinSxS\pending.xml
* an active BITS transfer
* a reparse point (junction or directory symlink) anywhere in the tree, because
both Remove-Item -Recurse and robocopy /MIR delete through one
- Advisory warnings block the run unless -IgnoreAdvisories is passed. CBS
PackagesPending is advisory, not a hard stop, because it is a documented sticky key
that can survive reboots and would otherwise lock a stuck machine out forever.
- Services are recorded in a script-scope collection BEFORE each stop is attempted, and
a finally block restarts everything that had been running, in reverse order. This
covers a normal exit, an early return, an exception and a declined confirmation.
It is best effort for Ctrl+C: PowerShell normally runs finally blocks on a stop
request, but a hard kill of the host cannot be caught. A .bak left on disk and a
stopped service are both visible and recoverable, which is why the rename path exists.
- A service that will not stop aborts the run before anything is renamed or deleted.
- A service that will not start again makes the exit code non-zero.
.PARAMETER Elevated
Internal. Set automatically when the script relaunches itself elevated. Do not pass it
by hand.
.PARAMETER Execute
Perform the work. Without this the script reports what it would delete and changes
nothing.
.PARAMETER DryRun
Explicitly request a dry run. This is the default. Cannot be combined with -Execute.
.PARAMETER Full
Also clear DataStore. This resets the Windows Update database. You lose update history,
the hidden and declined update list, the cached WSUS or Intune deployment metadata and
the last scan cookie, and the per-update retry and backoff state. Requires an
interactive confirmation that no switch suppresses, so it cannot be combined with
-NonInteractive. Cannot be combined with -OlderThanDays.
.PARAMETER OlderThanDays
Only delete a top level item whose subtree has not been written to for this many days.
The filter uses the MAXIMUM LastWriteTime found anywhere under the item, not the
timestamp on the folder itself, because a folder's own timestamp does not change when a
file inside it is written. 0 (default) selects the whole folder and enables the
rename-then-delete path.
.PARAMETER NonInteractive
Suppress confirmation prompts. Does not override a hard stop and does not override an
advisory warning; use -IgnoreAdvisories for that. Cannot be combined with -Full.
.PARAMETER IgnoreAdvisories
Continue past advisory warnings, for example a check that could not run. Never
overrides a hard stop.
.PARAMETER NoUI
Never show the option dialog. The dialog already suppresses itself when the session is
not interactive, when -NonInteractive is given, or when a mode was named on the command
line, so this is only needed to force the command line path in an interactive session.
.PARAMETER ServiceTimeoutSeconds
How long to wait for each service to stop or start. Default 60.
.PARAMETER LogDirectory
Where to write the transcript. Default %SystemRoot%\Logs\SoftwareDistributionCleanup.
An interactive relaunch keeps its console open, but the transcript is the durable
record and is the only output an automated run leaves behind.
.EXAMPLE
.\SoftwareDistributionCleanup.ps1
Dry run. Reports what would be deleted and how much space it would free.
.EXAMPLE
.\SoftwareDistributionCleanup.ps1 -Execute
Renames Download out of the way, restarts Windows Update within milliseconds, then
deletes the renamed folder. Prompts once before starting.
.EXAMPLE
.\SoftwareDistributionCleanup.ps1 -Execute -NonInteractive -OlderThanDays 30
Unattended. Deletes only payloads whose subtree has been untouched for 30 days, in
place, and leaves recent ones alone.
.EXAMPLE
.\SoftwareDistributionCleanup.ps1 -Execute -Full
Full reset of the Windows Update working area, including the update database. Asks for
a typed confirmation first.
.NOTES
Created by Jauder Ho
Last modified 9/11/2026
https://www.carumba.com
BSD License
Pull requests are welcome.
Requires PowerShell 5.1 or later and an elevated session.
Service set. UsoSvc (Update Orchestrator) is stopped first, because it will restart
wuauserv underneath a run that ignores it. DoSvc (Delivery Optimization) is the default
download transport on current Windows 10 and 11, so BITS alone is not enough. msiserver
and cryptsvc are deliberately NOT stopped: msiserver is demand-start, so it running
means an MSI transaction is in flight right now, and cryptsvc only belongs in the
documented reset because that procedure renames catroot2, which this script never
touches.
The BITS check is defence in depth only. A Delivery Optimization download produces no
BITS job at all, and -AllUsers does not reliably surface a SYSTEM session 0 job. The
Windows Update Agent query is the check that actually answers the question. If
Get-BitsTransfer reports an error it is shown and treated as an advisory, so a run that
could not perform the check is never reported as one that passed it.
SusClientId, deferral and pause policy, and active hours live in the registry and are
not touched by anything here.
Structure and the robocopy long-path idea follow Tom de Leeuw's Clear-SoftwareDistribution
from the ComputerCleanup module.
.LINK
https://learn.microsoft.com/en-us/troubleshoot/windows-client/installing-updates-features-roles/additional-resources-for-windows-update
.LINK
https://www.powershellgallery.com/packages/ComputerCleanup
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
[switch]$Elevated,
[switch]$Execute,
[switch]$DryRun,
[switch]$Full,
[ValidateRange(0, 3650)]
[int]$OlderThanDays = 0,
[switch]$NonInteractive,
[switch]$IgnoreAdvisories,
[switch]$NoUI,
[ValidateRange(5, 600)]
[int]$ServiceTimeoutSeconds = 60,
[string]$LogDirectory = (Join-Path $env:SystemRoot 'Logs\SoftwareDistributionCleanup')
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$script:SoftwareDistribution = Join-Path $env:SystemRoot 'SoftwareDistribution'
$script:DownloadPath = Join-Path $script:SoftwareDistribution 'Download'
$script:DataStorePath = Join-Path $script:SoftwareDistribution 'DataStore'
# Absolute path. An elevated session must not resolve a tool through $env:PATH.
$script:Robocopy = Join-Path $env:SystemRoot 'System32\robocopy.exe'
# Stop order. The orchestrator goes first so it cannot restart the ones below it, and the
# transports go last. Restart order is the reverse of the order things were recorded in.
$script:StopOrder = @('UsoSvc', 'wuauserv', 'DoSvc', 'bits')
# Names a backup folder from this script can have, so an interrupted run is recognisable.
$script:BackupPattern = '^(Download|DataStore)\.\d{8}-\d{6}(-\d+)?\.bak$'
# A run of failures this long is a systemic problem, not a long-path problem.
$script:MaxConsecutiveFailures = 10
# How many items a dry run prints before it summarises. The rest go to the verbose stream.
$script:PreviewCount = 20
# Caller owned service state. Every entry is appended BEFORE the matching stop is
# attempted, so an interruption between the append and the stop still leaves the finally
# block something to restore. Safety state is never round-tripped through a return value.
$script:ServiceState = New-Object System.Collections.ArrayList
$script:RestoreFailures = 0
# One empty directory per run for the robocopy mirror-from-empty fallback.
$script:EmptyMirrorSource = $null
# ---------------------------------------------------------------------------
# Output helpers
# ---------------------------------------------------------------------------
$script:UseColor = [string]::IsNullOrEmpty($env:NO_COLOR)
function Write-Log {
param(
[Parameter(Mandatory = $true)][AllowEmptyString()][string]$Message,
[ValidateSet('Info', 'Good', 'Warn', 'Bad', 'Step')][string]$Level = 'Info'
)
$prefix = @{ Info = ' '; Good = '[+]'; Warn = '[!]'; Bad = '[x]'; Step = '==>' }[$Level]
$color = @{ Info = 'Gray'; Good = 'Green'; Warn = 'Yellow'; Bad = 'Red'; Step = 'Cyan' }[$Level]
if ($script:UseColor) {
Write-Host ('{0} {1}' -f $prefix, $Message) -ForegroundColor $color
}
else {
Write-Host ('{0} {1}' -f $prefix, $Message)
}
}
function Format-Bytes {
param([AllowNull()][Nullable[long]]$Bytes)
if ($null -eq $Bytes) { return 'n/a' }
$sign = ''
$value = [double]$Bytes
if ($value -lt 0) {
$sign = '-'
$value = -$value
}
foreach ($unit in @('B', 'KB', 'MB', 'GB', 'TB')) {
if ($value -lt 1024 -or $unit -eq 'TB') {
return ('{0}{1:N2} {2}' -f $sign, $value, $unit)
}
$value = $value / 1024
}
}
function Get-SystemDriveFree {
$drive = Get-PSDrive -Name ($env:SystemDrive.TrimEnd(':')) -ErrorAction SilentlyContinue
if ($drive -and $null -ne $drive.Free) { return [long]$drive.Free }
return $null
}
# ---------------------------------------------------------------------------
# Argument validation. Deliberately ahead of the elevation block: an invalid invocation
# must fail in the caller's console, not behind a UAC prompt in a window that closes.
# ---------------------------------------------------------------------------
function Test-ParameterCombination {
<#
Returns a message describing the first invalid combination, or $null when the
current settings are coherent. Called before elevation, and again after the option
dialog, because the dialog can change what was chosen.
#>
if ($Execute -and $DryRun) {
return '-Execute and -DryRun are mutually exclusive.'
}
if ($Full -and $OlderThanDays -gt 0) {
return '-Full and -OlderThanDays are mutually exclusive. The update database is not cleared selectively.'
}
if ($Full -and $NonInteractive) {
return '-Full and -NonInteractive are mutually exclusive. Clearing DataStore requires a typed confirmation that no switch suppresses.'
}
if ($LogDirectory -match '"') {
return '-LogDirectory must not contain a double quote character.'
}
return $null
}
$invalid = Test-ParameterCombination
if ($invalid) {
Write-Error $invalid
exit 1
}
# ---------------------------------------------------------------------------
# Elevation. Matches the pattern used by the other scripts in this repo.
# https://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator
# ---------------------------------------------------------------------------
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function ConvertTo-QuotedArgument {
<#
CommandLineToArgvW treats a run of backslashes immediately before a closing quote
as escapes, so "D:\WU Logs\" would swallow the quote and corrupt the rest of the
child command line. Doubling that trailing run is the documented fix.
#>
param([Parameter(Mandatory = $true)][AllowEmptyString()][string]$Value)
$escaped = $Value
if ($escaped -match '(\\+)$') {
$escaped = $escaped + $Matches[1]
}
return ('"{0}"' -f $escaped)
}
if ((Test-Admin) -eq $false) {
if ($Elevated) {
Write-Error 'Elevation was attempted and failed. Rerun from an elevated PowerShell session.'
exit 1
}
# Matches the elevation idiom used by the rest of this repo: -NoProfile -NoExit, fired
# and forgotten so UAC comes up immediately and no non-elevated console is left behind.
# -NoExit keeps the elevated window up so the operator can read the result.
$interactive = [Environment]::UserInteractive
$relaunch = @('-NoProfile', '-ExecutionPolicy', 'Bypass')
if ($interactive) { $relaunch += '-NoExit' }
$relaunch += @('-File', (ConvertTo-QuotedArgument $PSCommandPath), '-Elevated')
foreach ($entry in $PSBoundParameters.GetEnumerator()) {
if ($entry.Key -eq 'Elevated') { continue }
if ($entry.Value -is [switch]) {
# Emit the explicit boolean so -Confirm:$false and -Verbose:$false survive.
$relaunch += ('-{0}:${1}' -f $entry.Key, $entry.Value.IsPresent)
}
else {
$relaunch += ('-{0}' -f $entry.Key)
$relaunch += (ConvertTo-QuotedArgument ([string]$entry.Value))
}
}
try {
if ($interactive) {
Start-Process powershell.exe -Verb RunAs -ArgumentList $relaunch | Out-Null
exit 0
}
# No console to read in an automated context, so wait and report the real result.
$childExit = 1
$child = Start-Process powershell.exe -Verb RunAs -ArgumentList $relaunch -PassThru -Wait
if ($null -ne $child) {
try {
if ($null -ne $child.ExitCode) { $childExit = [int]$child.ExitCode }
}
catch {
Write-Warning ('Could not read the exit code of the elevated run: {0}' -f $_.Exception.Message)
}
}
exit $childExit
}
catch {
Write-Error 'Elevation was declined or failed.'
exit 1
}
}
Write-Output 'Running with full privileges...'
# ---------------------------------------------------------------------------
# Preflight
# ---------------------------------------------------------------------------
function Test-ShouldShowDialog {
<#
The dialog appears only for an operator who launched the script without saying what
they wanted. An explicit mode on the command line, -NonInteractive, -NoUI, or a
non-interactive session all suppress it, so a scheduled task never blocks on a
window nobody is there to close.
#>
if ($NoUI) { return $false }
if ($NonInteractive) { return $false }
if (-not [Environment]::UserInteractive) { return $false }
foreach ($explicit in @('Execute', 'DryRun', 'Full')) {
if ($PSBoundParameters.ContainsKey($explicit)) { return $false }
}
return $true
}
function Show-OptionDialog {
<#
Presents the run options and writes the operator's choices back into the script
scope parameter variables. Returns $false if the operator cancelled.
Invalid combinations are made unreachable by enabling and disabling controls rather
than by rejecting the form afterwards, and Test-ParameterCombination still runs as
a backstop.
#>
try {
Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
Add-Type -AssemblyName System.Drawing -ErrorAction Stop
}
catch {
Write-Log ('Windows Forms is unavailable, so the option dialog is skipped: {0}' -f $_.Exception.Message) -Level Warn
Write-Log 'Continuing with command line settings (dry run unless -Execute was passed).' -Level Warn
return $true
}
[Windows.Forms.Application]::EnableVisualStyles()
$form = New-Object Windows.Forms.Form
$form.Text = 'SoftwareDistribution Cleanup'
$form.ClientSize = New-Object Drawing.Size(520, 545)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = 'FixedDialog'
$form.MaximizeBox = $false
$form.MinimizeBox = $false
# The UAC transition can leave a new window behind the console otherwise.
$form.Topmost = $true
$intro = New-Object Windows.Forms.Label
$intro.Location = New-Object Drawing.Point(12, 12)
$intro.Size = New-Object Drawing.Size(496, 46)
$intro.Text = "Clears the Windows Update download cache.`r`nWindows Update is stopped only for the moment it takes to rename the folder; the delete happens with it running again."
$form.Controls.Add($intro)
$modeBox = New-Object Windows.Forms.GroupBox
$modeBox.Text = 'Mode'
$modeBox.Location = New-Object Drawing.Point(12, 62)
$modeBox.Size = New-Object Drawing.Size(496, 80)
$form.Controls.Add($modeBox)
$radioDry = New-Object Windows.Forms.RadioButton
$radioDry.Text = 'Dry run - report only, change nothing (recommended first)'
$radioDry.Location = New-Object Drawing.Point(15, 22)
$radioDry.Size = New-Object Drawing.Size(465, 22)
$radioDry.Checked = -not $Execute
$modeBox.Controls.Add($radioDry)
$radioExecute = New-Object Windows.Forms.RadioButton
$radioExecute.Text = 'Execute - clear the cache'
$radioExecute.Location = New-Object Drawing.Point(15, 48)
$radioExecute.Size = New-Object Drawing.Size(465, 22)
$radioExecute.Checked = [bool]$Execute
$modeBox.Controls.Add($radioExecute)
$scopeBox = New-Object Windows.Forms.GroupBox
$scopeBox.Text = 'Scope'
$scopeBox.Location = New-Object Drawing.Point(12, 150)
$scopeBox.Size = New-Object Drawing.Size(496, 106)
$form.Controls.Add($scopeBox)
$chkFull = New-Object Windows.Forms.CheckBox
$chkFull.Text = 'Also clear DataStore - resets the update database'
$chkFull.Location = New-Object Drawing.Point(15, 22)
$chkFull.Size = New-Object Drawing.Size(465, 22)
$chkFull.Checked = [bool]$Full
$scopeBox.Controls.Add($chkFull)
$labelAge = New-Object Windows.Forms.Label
$labelAge.Text = 'Only delete items untouched for this many days (0 = everything):'
$labelAge.Location = New-Object Drawing.Point(15, 52)
$labelAge.Size = New-Object Drawing.Size(380, 22)
$scopeBox.Controls.Add($labelAge)
$numAge = New-Object Windows.Forms.NumericUpDown
$numAge.Location = New-Object Drawing.Point(400, 50)
$numAge.Size = New-Object Drawing.Size(80, 22)
$numAge.Minimum = 0
$numAge.Maximum = 3650
$numAge.Value = $OlderThanDays
$scopeBox.Controls.Add($numAge)
$ageNote = New-Object Windows.Forms.Label
$ageNote.Location = New-Object Drawing.Point(15, 76)
$ageNote.Size = New-Object Drawing.Size(465, 22)
$ageNote.Text = 'Above 0 switches to an in-place delete, holding the services down for longer.'
$scopeBox.Controls.Add($ageNote)
$overrideBox = New-Object Windows.Forms.GroupBox
$overrideBox.Text = 'Overrides - these reduce the safety margin'
$overrideBox.Location = New-Object Drawing.Point(12, 264)
$overrideBox.Size = New-Object Drawing.Size(496, 80)
$form.Controls.Add($overrideBox)
$chkNonInteractive = New-Object Windows.Forms.CheckBox
$chkNonInteractive.Text = 'Do not ask for confirmation'
$chkNonInteractive.Location = New-Object Drawing.Point(15, 22)
$chkNonInteractive.Size = New-Object Drawing.Size(465, 22)
$chkNonInteractive.Checked = [bool]$NonInteractive
$overrideBox.Controls.Add($chkNonInteractive)
$chkIgnore = New-Object Windows.Forms.CheckBox
$chkIgnore.Text = 'Continue past advisory warnings (never overrides a hard stop)'
$chkIgnore.Location = New-Object Drawing.Point(15, 48)
$chkIgnore.Size = New-Object Drawing.Size(465, 22)
$chkIgnore.Checked = [bool]$IgnoreAdvisories
$overrideBox.Controls.Add($chkIgnore)
$chkVerbose = New-Object Windows.Forms.CheckBox
$chkVerbose.Text = 'Verbose output - show every file and step in detail'
$chkVerbose.Location = New-Object Drawing.Point(15, 352)
$chkVerbose.Size = New-Object Drawing.Size(493, 22)
# Checked by default: these runs are long and mostly silent otherwise, and the
# step-by-step detail is what makes a failure diagnosable afterwards. An explicit
# -Verbose:$false on the command line still wins.
$chkVerbose.Checked = $true
if ($PSBoundParameters.ContainsKey('Verbose')) {
$chkVerbose.Checked = [bool]$PSBoundParameters['Verbose']
}
$form.Controls.Add($chkVerbose)
$notice = New-Object Windows.Forms.Label
$notice.Location = New-Object Drawing.Point(12, 380)
$notice.Size = New-Object Drawing.Size(496, 72)
$notice.ForeColor = [Drawing.Color]::FromArgb(150, 20, 20)
$form.Controls.Add($notice)
$buttonRun = New-Object Windows.Forms.Button
$buttonRun.Text = 'Run'
$buttonRun.Location = New-Object Drawing.Point(322, 505)
$buttonRun.Size = New-Object Drawing.Size(90, 28)
$buttonRun.DialogResult = [Windows.Forms.DialogResult]::OK
$form.Controls.Add($buttonRun)
$form.AcceptButton = $buttonRun
$buttonCancel = New-Object Windows.Forms.Button
$buttonCancel.Text = 'Cancel'
$buttonCancel.Location = New-Object Drawing.Point(418, 505)
$buttonCancel.Size = New-Object Drawing.Size(90, 28)
$buttonCancel.DialogResult = [Windows.Forms.DialogResult]::Cancel
$form.Controls.Add($buttonCancel)
$form.CancelButton = $buttonCancel
$refresh = {
$executing = $radioExecute.Checked
# -Full needs Execute, cannot be combined with an age filter, and always asks for a
# typed confirmation. Enforce all three by disabling rather than by failing later.
$chkFull.Enabled = $executing -and ($numAge.Value -eq 0)
if (-not $chkFull.Enabled) { $chkFull.Checked = $false }
$numAge.Enabled = -not $chkFull.Checked
$labelAge.Enabled = $numAge.Enabled
$ageNote.Enabled = $numAge.Enabled
$chkNonInteractive.Enabled = $executing -and -not $chkFull.Checked
if (-not $chkNonInteractive.Enabled) { $chkNonInteractive.Checked = $false }
$chkIgnore.Enabled = $executing
$lines = @()
if ($chkFull.Checked) {
$lines += 'Clearing DataStore loses update history, the hidden and declined update list, cached WSUS or Intune metadata, and per-update retry state. A typed confirmation is required.'
}
elseif ($executing -and $numAge.Value -gt 0) {
$lines += 'In-place delete. Windows Update stays stopped for the whole delete rather than milliseconds.'
}
elseif ($executing) {
$lines += 'Download is renamed out of the way, Windows Update restarts immediately, then the renamed folder is deleted.'
}
else {
$lines += 'Dry run. Nothing will be changed.'
}
$notice.Text = ($lines -join "`r`n")
}
$radioDry.Add_CheckedChanged($refresh)
$radioExecute.Add_CheckedChanged($refresh)
$chkFull.Add_CheckedChanged($refresh)
$numAge.Add_ValueChanged($refresh)
& $refresh
$answer = $form.ShowDialog()
$accepted = ($answer -eq [Windows.Forms.DialogResult]::OK)
if ($accepted) {
$script:Execute = [switch]$radioExecute.Checked
$script:DryRun = [switch]$radioDry.Checked
$script:Full = [switch]($chkFull.Enabled -and $chkFull.Checked)
$script:OlderThanDays = [int]$numAge.Value
$script:NonInteractive = [switch]($chkNonInteractive.Enabled -and $chkNonInteractive.Checked)
$script:IgnoreAdvisories = [switch]($chkIgnore.Enabled -and $chkIgnore.Checked)
# Dynamic scoping puts this in reach of Write-Verbose everywhere in the script.
if ($chkVerbose.Checked) { $script:VerbosePreference = 'Continue' }
else { $script:VerbosePreference = 'SilentlyContinue' }
}
$form.Dispose()
return $accepted
}
function Get-PendingState {
<#
Splits the pending-servicing keys into hard stops and advisories.
PackagesPending is advisory. It is a documented sticky key that can survive a
reboot, so treating it as a hard stop locks a machine with stuck CBS state out of
this script permanently.
#>
$hard = @()
$advisory = @()
$cbs = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing'
foreach ($key in @('RebootPending', 'RebootInProgress')) {
if (Test-Path -LiteralPath (Join-Path $cbs $key)) {
$hard += ('Component Based Servicing\{0}' -f $key)
}
}
if (Test-Path -LiteralPath (Join-Path $cbs 'PackagesPending')) {
$advisory += 'Component Based Servicing\PackagesPending is set. That key is frequently stale and survives reboots, so it is treated as advisory here.'
}
if (Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') {
$hard += 'WindowsUpdate\Auto Update\RebootRequired'
}
if (Test-Path -LiteralPath (Join-Path $env:SystemRoot 'WinSxS\pending.xml')) {
$hard += 'WinSxS\pending.xml'
}
return [pscustomobject]@{
HardStop = @($hard)
Advisory = @($advisory)
}
}
function Get-StagedUpdate {
<#
The check that actually answers "is it safe to delete Download". An update that is
already downloaded and waiting to install has its payload sitting in Download, and
sets none of the pending-reboot keys. This is the real hard stop.
Online = $false means the local cache only. No network call is made.
Returns Status = None | Staged | Unavailable. Unavailable is never a silent pass.
#>
$titles = @()
try {
$session = New-Object -ComObject 'Microsoft.Update.Session'
$searcher = $session.CreateUpdateSearcher()
$searcher.Online = $false
$result = $searcher.Search('IsInstalled=0 and IsHidden=0')
foreach ($update in $result.Updates) {
if ($update.IsDownloaded) { $titles += [string]$update.Title }
}
}
catch {
return [pscustomobject]@{
Status = 'Unavailable'
Count = 0
Titles = @()
Message = $_.Exception.Message
}
}
$status = 'None'
if ($titles.Count -gt 0) { $status = 'Staged' }
return [pscustomobject]@{
Status = $status
Count = $titles.Count
Titles = @($titles)
Message = ''
}
}
function Get-ServiceStartType {
param([Parameter(Mandatory = $true)]$Service)
try {
return [string]$Service.StartType
}
catch {
Write-Verbose ('ServiceController.StartType unavailable for {0}: {1}' -f $Service.Name, $_.Exception.Message)
}
try {
$wmi = Get-CimInstance -ClassName Win32_Service -Filter ("Name='{0}'" -f $Service.Name) -ErrorAction Stop
return [string]$wmi.StartMode
}
catch {
return 'Unknown'
}
}
function Test-Preflight {
Write-Log 'Running preflight checks...' -Level Step
$hardStop = @()
$advisory = @()
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
$hardStop += 'This script only runs on Windows.'
}
if (-not (Test-Path -LiteralPath $script:SoftwareDistribution)) {
$hardStop += ('{0} does not exist.' -f $script:SoftwareDistribution)
}
if (-not (Test-Path -LiteralPath $script:Robocopy)) {
$hardStop += ('{0} is missing. It is required for the delete path.' -f $script:Robocopy)
}
# The real gate. A downloaded, ready-to-install update is what Download is holding.
$staged = Get-StagedUpdate
switch ($staged.Status) {
'Staged' {
$hardStop += ('{0} update(s) are downloaded and waiting to install. Deleting Download now discards their payload. Install them, or cancel them in Windows Update, then rerun.' -f $staged.Count)
foreach ($title in $staged.Titles) { Write-Log ('staged: {0}' -f $title) -Level Bad }
}
'Unavailable' {
Write-Log ('The Windows Update Agent query could not run: {0}' -f $staged.Message) -Level Warn
$advisory += 'Could not confirm that no update is downloaded and waiting to install. This is the most important check in this script.'
}
}
$pending = Get-PendingState
if ($pending.HardStop.Count -gt 0) {
$hardStop += ('Servicing is mid-flight ({0}). Reboot and rerun.' -f ($pending.HardStop -join ', '))
}
foreach ($reason in $pending.Advisory) { $advisory += $reason }
# CBS mid-transaction reads payload straight out of Download. No switch overrides this,
# which matches how ComponentCleanup.ps1 treats concurrent servicing.
$worker = @(Get-Process -Name 'TiWorker' -ErrorAction SilentlyContinue)
if ($worker.Count -gt 0) {
$hardStop += ('The servicing worker (TiWorker.exe, PID {0}) is running, so CBS is mid-transaction. Wait for it to finish and rerun.' -f ($worker.Id -join ', '))
}
# Defence in depth only. Delivery Optimization downloads produce no BITS job, so a
# clean result here does not mean nothing is downloading.
try {
$jobs = @(Get-BitsTransfer -AllUsers -ErrorAction Stop |
Where-Object { @('Transferring', 'Connecting', 'Queued', 'TransientError') -contains [string]$_.JobState })
if ($jobs.Count -gt 0) {
$hardStop += ('BITS has {0} active transfer(s). Let them finish or cancel them first.' -f $jobs.Count)
}
}
catch {
Write-Log ('The BITS transfer check could not run: {0}' -f $_.Exception.Message) -Level Warn
$advisory += 'The BITS transfer check did not run, so an in-flight BITS download cannot be ruled out.'
}
foreach ($serviceName in $script:StopOrder) {
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($null -eq $service) {
if ($serviceName -eq 'wuauserv') {
$hardStop += 'The Windows Update service (wuauserv) is not present on this system.'
}
else {
Write-Verbose ('Service {0} is not present on this system.' -f $serviceName)
}
continue
}
# A Running service whose start type is Disabled cannot be started again once this
# script stops it. Say so before anything is deleted, not afterwards.
if ((Get-ServiceStartType -Service $service) -eq 'Disabled' -and [string]$service.Status -ne 'Stopped') {
$advisory += ('{0} is {1} but its start type is Disabled, so it cannot be started again after this run. Fix it first with: Set-Service -Name {0} -StartupType Manual' -f $serviceName, $service.Status)
}
}
# Checked here, before anything is renamed or stopped, rather than only at deletion
# time. A reparse point makes the tree undeletable by either robocopy /MIR or
# Remove-Item -Recurse without destroying whatever it points at, so there is no point
# taking the services down to rename a folder that cannot then be removed.
foreach ($path in @($script:DownloadPath, $script:DataStorePath)) {
if (-not (Test-Path -LiteralPath $path)) { continue }
if ($path -eq $script:DataStorePath -and -not $Full) { continue }
$links = @(Find-ReparsePoint -Path $path)
if ($links.Count -gt 0) {
foreach ($link in $links) { Write-Log ('reparse point: {0}' -f $link.FullName) -Level Bad }
$hardStop += ('{0} contains a reparse point. Both robocopy /MIR and Remove-Item -Recurse delete through one to whatever it targets. Check where it points and remove it by hand first.' -f $path)
}
}
foreach ($reason in $hardStop) { Write-Log $reason -Level Bad }
foreach ($reason in $advisory) { Write-Log $reason -Level Warn }
if ($hardStop.Count -gt 0) {
Write-Log 'Hard stop. No switch overrides the reasons above.' -Level Bad
return $false
}
if ($advisory.Count -gt 0 -and $Execute -and -not $IgnoreAdvisories) {
Write-Log 'Advisory warnings above. Rerun with -IgnoreAdvisories to proceed anyway.' -Level Bad
return $false
}
Write-Log 'Preflight passed.' -Level Good
return $true
}
# ---------------------------------------------------------------------------
# Service handling
#
# Every record is appended before the corresponding stop is attempted, and the finally
# block reads $script:ServiceState directly.
# ---------------------------------------------------------------------------
function Add-ServiceStateRecord {
<#
Records one service. Returns $true when the record is new, so the dependent walk
can use it as its recursion guard.
#>
param([Parameter(Mandatory = $true)]$Service)
foreach ($existing in $script:ServiceState) {
if ($existing.Name -eq $Service.Name) { return $false }
}
# The stop decision and the restore decision are different questions. StartPending and
# ContinuePending are on their way to Running, so they are restored; StopPending and
# Paused are not, because putting them back to Running would change the machine's state.
$status = [string]$Service.Status
$wasRunning = (@('Running', 'StartPending', 'ContinuePending') -contains $status)
[void]$script:ServiceState.Add([pscustomobject]@{
Name = [string]$Service.Name
Status = $status
WasRunning = $wasRunning
StartType = (Get-ServiceStartType -Service $Service)
})
return $true
}
function Register-DependentService {
<#
Stop-Service -Force stops the whole dependent tree as a side effect. Those services
are recorded here, before the stop, so they are restarted afterwards. The dedupe in
Add-ServiceStateRecord doubles as the recursion guard.
#>
param([Parameter(Mandatory = $true)]$Service)
$dependents = @()
try {
$dependents = @($Service.DependentServices)
}
catch {
Write-Log ('Could not enumerate the services that depend on {0}: {1}' -f $Service.Name, $_.Exception.Message) -Level Warn
return
}
foreach ($dependent in $dependents) {
if ([string]$dependent.Status -eq 'Stopped') { continue }
if (Add-ServiceStateRecord -Service $dependent) {
Write-Log ('{0} depends on {1} and will be stopped with it.' -f $dependent.Name, $Service.Name) -Level Warn
Register-DependentService -Service $dependent
}
}
}
function Stop-UpdateService {
<#
Stops each named service in order. Returns $true only when every service that is
present is confirmed Stopped. The caller must abort on $false rather than delete
files underneath a running service.
#>
param([Parameter(Mandatory = $true)][string[]]$Name)
$timeout = [timespan]::FromSeconds($ServiceTimeoutSeconds)
foreach ($serviceName in $Name) {
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($null -eq $service) {
Write-Verbose ('Service {0} is not present. Skipping.' -f $serviceName)
continue
}
Register-DependentService -Service $service
[void](Add-ServiceStateRecord -Service $service)
# Anything that is not Stopped needs stopping. StartPending, StopPending, Paused
# and ContinuePending are all live states with open handles.
if ([string]$service.Status -eq 'Stopped') {
Write-Log ('{0} is already stopped.' -f $serviceName)
continue
}
Write-Log ('Stopping {0} (currently {1})...' -f $serviceName, $service.Status) -Level Step
try {
Stop-Service -Name $serviceName -Force -ErrorAction Stop
$service.WaitForStatus('Stopped', $timeout)
}
catch {
Write-Log ('{0} would not stop: {1}' -f $serviceName, $_.Exception.Message) -Level Bad
return $false
}
$service.Refresh()
if ([string]$service.Status -ne 'Stopped') {
Write-Log ('{0} is {1}, not Stopped. Refusing to continue.' -f $serviceName, $service.Status) -Level Bad
return $false
}
}
return $true
}
function Get-ServiceNotStopped {
<#
Re-reads the services immediately before the destructive step. The orchestrator can
bring one back between the stop and the rename.
#>
param([Parameter(Mandatory = $true)][string[]]$Name)
$offenders = @()
foreach ($serviceName in $Name) {
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($null -eq $service) { continue }
if ([string]$service.Status -ne 'Stopped') {
$offenders += ('{0} ({1})' -f $serviceName, $service.Status)
}
}
return $offenders
}
function Restore-UpdateService {
<#
Restarts everything that had been running, newest record first, which is the reverse
of both the stop order and the dependent-before-parent recording order. Always
called from a finally block. Recomputes $script:RestoreFailures from scratch so
calling it twice cannot double count.
#>
$failures = 0
$timeout = [timespan]::FromSeconds($ServiceTimeoutSeconds)
for ($index = $script:ServiceState.Count - 1; $index -ge 0; $index--) {
$entry = $script:ServiceState[$index]
if (-not $entry.WasRunning) { continue }
$service = Get-Service -Name $entry.Name -ErrorAction SilentlyContinue
if ($null -eq $service) {
$failures++
Write-Log ('{0} has disappeared and cannot be restarted.' -f $entry.Name) -Level Bad
continue
}
if ([string]$service.Status -eq 'Running') { continue }
if ($entry.StartType -eq 'Disabled') {
$failures++
Write-Log ('{0} is Disabled and cannot be started. Run: Set-Service -Name {0} -StartupType Manual; Start-Service -Name {0}' -f $entry.Name) -Level Bad
continue
}
Write-Log ('Starting {0}...' -f $entry.Name) -Level Step
try {
Start-Service -Name $entry.Name -ErrorAction Stop
$service.WaitForStatus('Running', $timeout)
Write-Log ('{0} is running again.' -f $entry.Name) -Level Good
}
catch {
$failures++
Write-Log ('{0} did not restart: {1}. Start it manually.' -f $entry.Name, $_.Exception.Message) -Level Bad
}
}
$script:RestoreFailures = $failures
}
# ---------------------------------------------------------------------------
# Measuring and inspection
# ---------------------------------------------------------------------------
function Find-ReparsePoint {
<#
Returns up to $Limit reparse points (junctions and directory symlinks) at or below
$Path, including $Path itself.
This is mandatory before any robocopy /MIR. /MIR walks reparse points in the
DESTINATION and deletes through them to whatever they point at; /XJ only protects
the source side. Windows PowerShell 5.1's Remove-Item -Recurse has the same
problem. The ReparsePoint attribute bit is tested directly, because LinkType and
Target are $null for system-defined junctions.
Select-Object -First stops the upstream enumeration, so a junction loop cannot run
this away.
#>
param(
[Parameter(Mandatory = $true)][string]$Path,
[int]$Limit = 10
)
$found = @()
if (-not (Test-Path -LiteralPath $Path)) { return $found }
try {
$root = Get-Item -LiteralPath $Path -Force -ErrorAction Stop