PowerShell

How to automate Windows Update with PowerShell and Task Scheduler

In order to orchestrate Windows Update with PowerShell, the module “PSWindowsUpdate” needs to be installed. This module contains cmdlets to manage the Windows Update Client (severs). This module is vital because it helps automate the deployment of Windows Update using the
– “Invoke-WUInstall” command to install Windows Updates remotely on computers or with the following below that can be configured via task scheduler to automate updates on individual computers.
– “Install-WindowsUpdate” for installing updates from Microsoft Update Center or with the
– “Get-WindowsUpdate” to update Windows from WSUS.

Note: This module is not installed by default on Windows Servers and when installed it contains a set of functions to check, download and install updates from PowerShell.

First, Install the PSWindowsUpdateModule: To have the module installed in PowerShell 5 and above, use the cmdlets below.

Install-Module PSWindowsUpdate

Note: To automate this installation in an unattended (silent) mode, use the command by adding the “-Force” flag as shown below

Install-Module PSWindowsUpdate -Force

To list all the module installed, use the command below

Get-Command –module PSWindowsUpdate

Next, run the command below. If the updates are not already downloaded, the command will contact the WSUS and pull the updates, have the updates installed and restart the server.

Here is the meaning of the module command.
Get-WUInstall, Install-WindowsUpdate (alias for Get-WindowsUpdate –Install) – install updates

Microsoft Updates: For Updates directly from Microsoft update center, use the command below.

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot

The command below might not work correctly, because of this, the “install-WindowsUpdate” is my desired choice.

Get-WUInstall –MicrosoftUpdate –AcceptAll –AutoReboot

To have this run at a specific period of time, create PowerShell script and create a scheduled task to automate Windows Update with the code below.

Install-Module PSWindowsUpdate -Force
#installs everything (newest version) along with required modules.

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
#Will ensure that updates are downloaded, installed completely and then restarted.

The AcceptAll key accepts installation of all update packages, and AutoReboot allows Windows to automatically restart after the updates are installed.

WSUS Updates (Windows Server Update Services): For WSUS updates, the following commands works correctly.

Get-WindowsUpdate -install -AcceptAll -AutoReboot

Next steps! Have your script automated
– Create a scheduled task, see the following link below for more details https://techdirectarchive.com/2020/03/24/how-to-create-a-scheduled-task-on-windows-server-2019/

For Task Scheduled task error and success code, see https://techdirectarchive.com/2020/03/24/task-scheduler-errors-and-success-code-what-does-code-0x41301-mean/

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x