Skip to content

How to install Windows updates using PowerShell

Jordan Hammond fun headshot
Jordan Hammond|April 28, 2020
Generic blog header
Generic blog header

Does Christmas come twice a year? If you installed the latest PDQ Inventory to get access to the PowerShell Scanner, then the answer is an absolute “yes it does.” Now that you have your hands on it, there are so many possibilities; it can be overwhelming. Why not let the sweet scripting of Nate and Colby get you started? They have quite a bit on our GitHub, and if there is something you feel like people can use, please make a pull request, and we can get it added. Out of all of the scripts there, let’s focus on ones involving Windows Updates.

Importing Required Modules

The pswindowsupdate module in PowerShell is not installed by default, so we would need to do this first to make sure the commands we use will be available. In a perfect world, you could just run a quick Install-Module and be done with it, and while the idea for that is good, there are some things to watch out for. It may not know where to look, or it may run into several issues. The first is that when importing the module, it will print out some lines saying it was done, which would return in a result and muddy up your results. So you will want to cast that to $null.

$null = Import-Module $ModuleName -Force

The second issue is that if it is already loaded, or it does not know where to look, you could get some errors. To avoid that, we use Try/Catch to test and handle those. So instead of the quick one-liner, to ensure we are not getting an error, we would want to put it all together in the script we have below.

[CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String] $ModuleName ) # Install the module if it is not already installed, then load it. Try { $null = Get-InstalledModule $ModuleName } Catch { if ( -not ( Get-PackageProvider -ListAvailable | Where-Object Name -eq "Nuget" ) ) { $null = Install-PackageProvider "Nuget" -Force } $null = Install-Module $ModuleName -Force } $null = Import-Module $ModuleName -Force

Getting your Update Data

To make sure you don’t have to copy this full code, I recommend you follow these instructions for downloading and importing from the GitHub repository. You are creating a script to let you know what updates are missing when you last updated or getting an update history are all one quick command. 

I did include the full import script here, but I would strongly recommend if you plan to use these that you get them from the GitHub, so if updates are made, you can keep your scanners up to date with a quick command.

Conclusion

Hopefully, this helps get the creative juices going, and you can dive into your next bit of PowerShell brilliance. This is a fantastic tool that can make you seem like an IT superstar to your peers. Dive in and have fun!

Jordan Hammond fun headshot
Jordan Hammond

Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet.

Related articles