Install Windows Updates Using PowerShell

Recently, I was required to update Windows on multiple systems. PowerShell is the best option for this. In this tutorial, I will explain how to install Windows updates using PowerShell.

Unlike the traditional Windows Update GUI, PowerShell lets you script, schedule, and automate the entire update process. This is especially useful for system administrators managing multiple machines or users who want fine-grained control over updates.

Prerequisites

You should have these prerequisites before installing Windows updates using PowerShell. So, before you begin, make sure:

  • You are running PowerShell as Administrator.
  • Your system is connected to the internet.
  • Windows Update is not disabled via Group Policy.
  • You have PowerShell 5.1 or later (comes by default on Windows 10/11).

Method 1: Using the PSWindowsUpdate Module

The PSWindowsUpdate module is a community module that simplifies the process of managing Windows updates via PowerShell. Follow these steps to install the module:

Step 1: Install the PSWindowsUpdate Module

Run this command to install the module from the PowerShell Gallery:

Install-Module -Name PSWindowsUpdate -Force

Note: If it prompts you to trust the repository, type Y and hit Enter.

Step 2: Import the Module

Once installed, import the module to your PowerShell session:

Import-Module PSWindowsUpdate

Step 3: Check for Available Updates

To check for available updates in Windows, use the following command:

Get-WindowsUpdate

This command will list all available updates for your system without installing.

It will show the Windows updates like below:

Install Windows Updates Using PowerShell

Step 4: Install Updates

To install all available updates, use the Install-WindowsUpdate cmdlet:

Install-WindowsUpdate -AcceptAll -AutoReboot
  • -AcceptAll: Automatically accepts all updates.
  • -AutoReboot: Automatically restarts if required after updates.

This command will automatically accept all updates and reboot the system if necessary. If you prefer to install specific updates, use the -KBArticleID parameter:

Install-WindowsUpdate -KBArticleID KB5012599, KB5012598 -AcceptAll -AutoReboot

Check out Get Windows Update History Using PowerShell

Schedule Windows Updates using PowerShell

You can schedule updates to run at a specific time using the Task Scheduler. Here’s how to create a scheduled task using PowerShell:

  1. Create a PowerShell Script: Save the following script as UpdateScript.ps1:
   Import-Module PSWindowsUpdate
   Install-WindowsUpdate -AcceptAll -AutoReboot
  1. Create a Scheduled Task: Use the following command to create a scheduled task that runs the script daily at 3 AM:
   $Action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-File "C:\Path\To\UpdateScript.ps1"'
   $Trigger = New-ScheduledTaskTrigger -Daily -At 3AM
   $Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
   Register-ScheduledTask -TaskName "DailyWindowsUpdate" -Action $Action -Trigger $Trigger -Principal $Principal

Handling Update Logs Using PowerShell

To review the update logs, use the Get-WindowsUpdateLog cmdlet:

Get-WindowsUpdateLog -LogPath "C:\Windows\Logs\WindowsUpdate.log"

This command will export the update logs to the specified path, allowing you to review the update process and troubleshoot any issues.

Check out Install RSAT in Windows 11 Using PowerShell

Update Multiple Machines Using PowerShell

Imagine you are a system administrator managing a network of computers in a corporate office. You need to ensure all machines receive the latest security updates without disrupting the workflow. Here’s how you can achieve this using PowerShell:

  1. Create a List of Computers: Save the list of computer names in a text file called Computers.txt:
   NY-Office-PC1
   NY-Office-PC2
   NY-Office-PC3
  1. Create a PowerShell Script: Save the following script as UpdateAllMachines.ps1:
   $Computers = Get-Content "C:\Path\To\Computers.txt"
   foreach ($Computer in $Computers) {
       Invoke-Command -ComputerName $Computer -ScriptBlock {
           Import-Module PSWindowsUpdate
           Install-WindowsUpdate -AcceptAll -AutoReboot
       }
   }
  1. Run the Script: Execute the script from your administrator machine:
   .\UpdateAllMachines.ps1

This script will connect to each computer listed in Computers.txt and install all available updates. The Invoke-Command cmdlet allows you to run commands on remote computers, making managing updates across the entire network easy.

Check out Restart a Windows Service Using PowerShell

Method 2: Using Windows Update Agent COM Object

This method doesn’t require any module and uses built-in Windows COM objects. It’s a bit more manual but good for environments where installing modules is restricted.

Step 1: Create Update Session

$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()

Step 2: Search for Updates

Below is how to search for updates.

$SearchResult = $UpdateSearcher.Search("IsInstalled=0")

Step 3: List Updates

Here is how to get the list of updates.

$SearchResult.Updates | ForEach-Object {
    Write-Output $_.Title
}

Step 4: Download and Install Updates

You can use the below PowerShell script.

$UpdatesToInstall = New-Object -ComObject Microsoft.Update.UpdateColl

foreach ($update in $SearchResult.Updates) {
    $UpdatesToInstall.Add($update) | Out-Null
}

$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToInstall
$Downloader.Download()

$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()

Check out Check for Windows Updates Using PowerShell

Get-WindowsUpdate : The term ‘Get-WindowsUpdate’ is not recognized as the name of a cmdlet, function, script file, or operable program

While running the Get-WindowsUpdate PowerShell cmdlet, you might get the below error:

Get-WindowsUpdate : The term ‘Get-WindowsUpdate’ is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again. At line:1 char:1 + Get-WindowsUpdate + ~~~~~ + CategoryInfo : ObjectNotFound: (Get-WindowsUpdate:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

You can even see the exact output in the screenshot below:

Get-WindowsUpdate The term Get-WindowsUpdate is not recognized

To fix this issue, you can run the below PowerShell cmdlets:

Install-Module -Name PSWindowsUpdate -Force
Import-Module PSWindowsUpdate

Conclusion

In this tutorial, I explained how to install Windows updates using PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.