This repository was archived by the owner on Mar 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathClear-WindowsUpdateCache.ps1
More file actions
41 lines (32 loc) · 1.44 KB
/
Clear-WindowsUpdateCache.ps1
File metadata and controls
41 lines (32 loc) · 1.44 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
# Clear-WindowsUpdateCache
# Script to clear the Windows Update Cache to free up disk space
function Get-FreeDiskSpace {
$OS = Get-WmiObject -Class Win32_OperatingSystem
$Disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='$($os.SystemDrive)'" |
Select @{Name="FreeGB";Expression={[math]::Round($_.FreeSpace / 1GB, 2)}}
return $Disk.FreeGB
}
# Get initial free disk space
$Before = Get-FreeDiskSpace
Write-Host "Free Disk Space before: $Before GB" -ForegroundColor Blue
# Check Windows Update Service status
$WUService = Get-Service wuauserv
# Stop Windows Update Service if it's running
if ($WUService.Status -eq "Running") {
Write-Host "Stopping Windows Update Service..." -ForegroundColor Blue
$WUService | Stop-Service -Force
}
# Clean Windows Update Cache
Write-Host "Cleaning Windows Update Cache..." -ForegroundColor Blue
$UpdateCachePath = Join-Path $env:windir "SoftwareDistribution\Download"
Get-ChildItem -Path $UpdateCachePath -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
# Start Windows Update Service
Write-Host "Starting Windows Update Service..." -ForegroundColor Blue
$WUService | Start-Service
# Get final free disk space
$After = Get-FreeDiskSpace
Write-Host "Free Disk Space after: $After GB" -ForegroundColor Blue
# Calculate and display the freed disk space
$Cleaned = $After - $Before
Write-Host "Cleaned: $Cleaned GB" -ForegroundColor Green
Write-Host "Done..." -ForegroundColor Green