One of the ways to ensure your windows update works is to clear the ‘SoftwareDistribution’ folder
'Fix-SoftwareDistribution.ps1'
#Fix the SoftwareDistribution folder
Stop-Service -Name wuauserv -verb
Stop-Service -Name cryptSvc -verb
Stop-Service -Name bits -verb
Stop-Service -Name msiserver -verb
# Define the path
$path = "C:\Windows\SoftwareDistribution\*"
# Get the current ACL for the folder
$acl = Get-Acl "C:\Windows\SoftwareDistribution" -verb
# Define the user or group to give permissions to
$user = "Everyone" # Replace with the desired user or group
# Create a new access rule
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, "FullControl", "Allow")
# Add the access rule to the ACL
$acl.SetAccessRule($accessRule)
# Apply the modified ACL to the folder
Set-Acl -Path "C:\Windows\SoftwareDistribution" -AclObject $acl -verb
# (Optional) To recursively apply to all contents
Get-ChildItem -Path "C:\Windows\SoftwareDistribution" -Recurse | Set-Acl -AclObject $acl -verb
#Remove "C:\Windows\SoftwareDistribution\*"
Remove-Item -Path "C:\Windows\SoftwareDistribution\*" -Recurse -Force -verb
Start-Service -Name wuauserv -verb
Start-Service -Name cryptSvc -verb
Start-Service -Name bits -verb
Start-Service -Name msiserver -verb

