List and install Microsoft Updates with PowerShell

It has become my goal to do as much server administration as I can via PowerShell. I want to do this mainly to force myself out of my comfort zone (the GUI) and increase my PowerShell knowledge. I recently installed a Windows Server 2016 server that by default comes without a GUI. One of the first things I needed to do was install updates to it. Of course I didn’t know how but I knew there was a module out there for it. Here is my process of how I worked out what to do and how you install Microsoft updates via PowerShell.

Firstly I knew there was a module in the PSGallery repository that was designed for Windows Updates. I didn’t know what it was called so I listed all the modules and sorted them by name.

This turned out to be a terrible idea as there were hundreds that I would need to scroll through and read before I could find the correct one. So I narrowed the search down by searching a few different terms but found what I wanted with this command:

Find-Module -Repository PSGallery -Name *WindowsUpdate* | Sort-Object Name

This brought up three modules but the one we want is PSWindowsUpdate.

Now that we know the module we need to install it.

Install-Module -Repository PSGallery -Name PSWindowsUpdate

Once installed we can view the new cmdlets that the PSWindowsUpdate module gives us, to do this we run:

Get-Command -Module PSWindowsUpdate

After reading the help for a few of the cmdlets I found one that will list the available updates for us:

Get-WUList -MicrosoftUpdate

This cmdlet actually brought up an error for me. It turns out I don’t have a ServiceID for Microsoft Update registered, I only have one for Windows Update. At this point we could change the parameter on the cmdlet to use -WindowsUpdate but I want all updates available to all Microsoft products not just Windows udpates so I needed to register the Microsoft Update serviceID:

Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d

Now when I do a Get-WUServiceManager we see both Windows Update and Microsoft Update listed:

Now we can continue with Get-WUList -MicrosoftUpdate which will list all available updates for our server:

Now we know whats available and that we want all of the updates to install we can go back to the cmdlets available for the module PSWindowsUpdate and we find that there is one called Get-WUInstall. We can use this cmdlet to download and install our updates:

If you don’t want any prompting you could use the following:

Get-WUinstall -MicrosoftUpdate -AcceptAll -AutoReboot

That is pretty much all there is to installing Windows Updates via PowerShell. If you get stuck the two best commands to help you work out what to do is Get-Help and Get-Command.

Thanks guys!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s