Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 527bbba

Browse files
authored
Simplifying the code
This improved script should be more readable and maintainable while still cleaning the Windows Update cache to free up disk space.
1 parent 4f50c1e commit 527bbba

File tree

1 file changed

+31
-69
lines changed

1 file changed

+31
-69
lines changed

Clear-WindowsUpdateCache.ps1

+31-69
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,41 @@
1-
##########
2-
# Script to Clear the Windows Update Cache to free up diskspace
3-
# Author: VirtualOx <[email protected]>
4-
# Version: v1.0.1, 2019-07-16
5-
# Source: https://github.com/virtualox/Clear-WindowsUpdateCache
6-
##########
7-
8-
function FreeDiskSpace() {
9-
$OS = Get-WMiobject -Class Win32_operatingsystem
10-
$Disk = Get-WMIObject Win32_Logicaldisk -filter "deviceid='$($os.systemdrive)'" |
11-
Select PSComputername,DeviceID,
12-
@{Name="FreeGB";Expression={[math]::Round($_.Freespace/1GB,2)}}
13-
return $Disk.FreeGB
1+
# Clear-WindowsUpdateCache
2+
# Script to clear the Windows Update Cache to free up disk space
3+
4+
function Get-FreeDiskSpace {
5+
$OS = Get-WmiObject -Class Win32_OperatingSystem
6+
$Disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='$($os.SystemDrive)'" |
7+
Select @{Name="FreeGB";Expression={[math]::Round($_.FreeSpace / 1GB, 2)}}
8+
return $Disk.FreeGB
149
}
1510

16-
function CheckWUS() {
17-
$s = Get-Service wuauserv
18-
if ($s.Status -eq "Running") {
19-
return 0
20-
} else {
21-
return 1
22-
}
23-
}
24-
function StopWUS() {
25-
Stop-Service wuauserv -Force
26-
}
11+
# Get initial free disk space
12+
$Before = Get-FreeDiskSpace
13+
Write-Host "Free Disk Space before: $Before GB" -ForegroundColor Blue
14+
15+
# Check Windows Update Service status
16+
$WUService = Get-Service wuauserv
2717

28-
function StartWUS() {
29-
Start-Service wuauserv
18+
# Stop Windows Update Service if it's running
19+
if ($WUService.Status -eq "Running") {
20+
Write-Host "Stopping Windows Update Service..." -ForegroundColor Blue
21+
$WUService | Stop-Service -Force
3022
}
3123

32-
function WUSRunning() {
33-
Write-Host "Windows Update Service is Running..." -ForegroundColor Red
34-
Write-Host " Stopping Windows Update Service..." -ForegroundColor Blue
24+
# Clean Windows Update Cache
25+
Write-Host "Cleaning Windows Update Cache..." -ForegroundColor Blue
26+
$UpdateCachePath = Join-Path $env:windir "SoftwareDistribution\Download"
27+
Get-ChildItem -Path $UpdateCachePath -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
3528

36-
# Stopping Windows Update Service and check again if it is stopped
37-
StopWUS
38-
if (CheckWUS) {
39-
WUSStopped
40-
} else {
41-
Write-Host "Can't stop Windows Update Service..." -ForegroundColor Red
42-
exit 1
43-
}
44-
Write-Host "Starting Windows Update Service..." -ForegroundColor Blue
45-
46-
# Starting the Windows Update Service again
47-
StartWUS
48-
}
49-
function WUSStopped() {
50-
Write-Host "Windows Update Service is Stopped..." -ForegroundColor Green
51-
52-
# Getting free disk space before the cleaning actions
53-
Write-Host " Free Disk Space before: " -ForegroundColor Blue -NoNewline
54-
$Before = FreeDiskSpace
55-
Write-Host "$Before GB"
56-
57-
Write-Host " Cleaning Files..." -ForegroundColor Blue -NoNewline
58-
Get-ChildItem -LiteralPath $env:windir\SoftwareDistribution\Download\ -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
59-
Write-Host "Done..." -ForegroundColor Green
29+
# Start Windows Update Service
30+
Write-Host "Starting Windows Update Service..." -ForegroundColor Blue
31+
$WUService | Start-Service
6032

61-
# Getting free disk space after the cleaning actions
62-
Write-Host " Free Disk Space after: " -ForegroundColor Blue -NoNewline
63-
$After = FreeDiskSpace
64-
Write-Host "$After GB"
33+
# Get final free disk space
34+
$After = Get-FreeDiskSpace
35+
Write-Host "Free Disk Space after: $After GB" -ForegroundColor Blue
6536

66-
# Calculating the free disk space difference
67-
Write-Host " Cleaned: " -ForegroundColor Blue -NoNewline
68-
$Cleaned = $After - $Before
69-
Write-Host "$Cleaned GB"
70-
}
37+
# Calculate and display the freed disk space
38+
$Cleaned = $After - $Before
39+
Write-Host "Cleaned: $Cleaned GB" -ForegroundColor Green
7140

72-
# Program
73-
if (CheckWUS) {
74-
WUSStopped
75-
} else {
76-
WUSRunning
77-
}
7841
Write-Host "Done..." -ForegroundColor Green
79-
exit 0

0 commit comments

Comments
 (0)