Fix Windows Update Issues using PowerShell Script

It has always been a big task for administrators to fix Windows Update issues in Windows 10, Windows 11, or Windows Server OSes. You may have tried the manual steps to fix Windows Update issues but it takes time. And if you have multiple systems to fix, it becomes a huge task. Here is where automation comes in handy. You can fix Windows Update issues using a PowerShell script with ease.

The PowerShell script we are going to share with you performs a 10-step troubleshooting. This includes both basic and advanced levels of operations to fix Windows Updates issues in Windows 10, 11, Windows Server 2012 R2, 2016, 2019, and 2022.

SEE ALSO: How to Open CMD or PowerShell in Current Folder in Windows?

PowerShell Script – Solve Windows Update Issues

Before we move on to the actual script, please note that this script is provided ‘AS IS’ without any warranties of any kind. However, this PowerShell script is intended to fix Windows Update issues and not create other problems. But it is still better to take a backup or create a system restore point.

Besides that, make sure to run this PowerShell script in the Administrative PowerShell console or Windows 11’s terminal to provide the script with the required rights.

Run The Powershell Script To Fix Windows Update Issues
Powershell Script to Fix Windows Update Issues

Let’s check the script functions step by step.

Step 1: Stop Windows Update Services

The first step in the script process is to stop all Windows update-related services. These services are necessary to stop in order to perform troubleshooting. Running services may cause further troubleshooting steps to fail.

Stop-WUServices $Services

The $Services variable holds the list of services that the Stop-WUServices (custom function) stops in the first step to fix Windows Update issues using the PowerShell script.


Step 2: Clearing the Windows Update database folders

Now, after stopping the services, it’s time to clear the Windows Update database folders. The following section of the script does the job. The PowerShell script handles the errors well so you won’t have to worry about them.

$Folders = @("$env:WINDIR\SoftwareDistribution", "$env:WINDIR\System32\catroot2")
ForEach ($Folder in $Folders) {
 Remove-Item "$Folder" -Recurse -Force 
}

Remember, this step will clear your Windows Update history from the Settings app. However, you can still get all the installed updates from the Control Panel or by using the Get-HotFix command in PowerShell.


SEE ALSO: How to Get Local User Accounts from Domain Computers using PowerShell?

Step 3: Removing QMGR data files

This step removes the old QMGR data files. These files sometimes cause issues with Windows Update. So, it’s a better idea to remove old QMGR data files to fix Windows Update issues.

Remove-Item "$env:ALLUSERSPROFILE\Application Data\Microsoft\Network\Downloader\qmgr.dat" -Force
Remove-Item "$env:ALLUSERSPROFILE\Microsoft\Network\Downloader\qmgr.dat" -Force

Step 4: Removing pending XML to fix Windows Update issues

Just like QMGR data files, the pending.xml file can also be one of the problem-causing components. It contains information about the pending packages. However, in some cases, the package information is not updated even after successful installation causing issues with future updates. So, to solve Windows update issues using the PowerShell script, we have to remove this file also.

$File = "$env:SystemRoot\WinSxS\pending.xml"
Take-Ownership $File
Remove-Item $File -Force

Here, we have used a custom Take-Ownership function to take ownership of inaccessible system files.


Step 5: Removing old Windows Update log

Just like the previous two steps, in this step, we will remove the WindowsUpdate.log file. Removing the existing file is however not recommended. But when the size of the file gets huge, it becomes harder to analyze the logs. You can always take a backup of this file should you need to review it for troubleshooting. In case you are not a pro, you can remove the file instead of taking a backup of it.

Remove-Item $env:SystemRoot\WindowsUpdate.log -Force

SEE ALSO: How to Create Bulk User in Active Directory using PowerShell script?

Step 6: Resetting Windows Update services security descriptors

Sometimes, malicious programs change the security descriptors or permissions of the Windows system services causing them to fail to start or behave in an unexpected manner. So, it would be better to reset the security descriptors to their default values to fix Windows update issues using the PowerShell script.

sc.exe sdset bits D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)
sc.exe sdset wuauserv D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)

Step 7: Registring Windows update DLLs again

Registering the system DLL files sometimes fixes the issues with Windows Update components. This script uses the regsvr32.exe command to re-register the DLLs.

regsvr32.exe /s atl.dll
regsvr32.exe /s urlmon.dll
regsvr32.exe /s mshtml.dll
regsvr32.exe /s shdocvw.dll
regsvr32.exe /s browseui.dll
.... more

Step 8: Deleting all BITS jobs to fix Windows Update issues

The BITS service transfers Windows Update-related data in the background. But sometimes these background jobs get stuck and cause issues for future updates. So, to fix this issue, this PowerShell script deletes the pending BITS jobs.

Get-BitsTransfer | Remove-BitsTransfer

SEE ALSO: Multi-Domain PowerShell Script for Admins to Reset User Account Passwords.

Step 9: Removing Staged Packages causing Windows Update issues

Some Windows packages or updates require the system reboot to install completely. These packages are marked as ‘Staged’ in the Windows registry. When these packages are not installed due to some problem, they cause issues for future Windows updates. So, in order to fix update issues, we must remove these ‘Staged’ packages.

$StgPkg = Get-WindowsPackage -Online | where {$_.PackageState -eq 'Staged'}
Remove-WindowsPackage -PackageName $Pkg.PackageName -Online –NoRestart

Step 10: Starting Windows update services again

Finally, it’s time to restart Windows update-related services again. This script uses a custom function Start-WUServices to start Windows update services.

Start-WUServices $Services

Download the PowerShell Script to fix Windows update issues

The above snippets are part of the PowerShell script hosted on GitHub. You can download the ResetWU script from this page. We request you to keep visiting the GitHub page frequently to download the latest script. Alternatively, you can click the button below to download the latest version of the PowerShell script.


SEE ALSO: How to Schedule Automatic Shutdown in Windows 10, 11?

Solve Windows Update Issues using PowerShell Script

You can use this PowerShell script to fix most of the common Windows update issues on Windows 10, 11, or Windows Server 2012 or newer. You may need to run the script multiple times to fix the issues. Also, make sure to restart your computer after running the script. Now, try to install the updates again.

We hope this script works for you. In case you have any issues with this script, make sure to comment below. You may also like to subscribe to our newsletter to get the latest updates via email.

Editorial Staff

Hi there, we are the editorial staff at WINDOSPC (former HELLPC). We are a team of funny and technical people. Feel free to get in touch with us via Contact-Us page.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.