Use a PowerShell Module to Run Windows Update

Use a PowerShell Module to Run Windows Update

  • Comments 8
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using a free Windows PowerShell module to run Windows Update on a computer.

Microsoft Scripting Guy, Ed Wilson, is here. One thing that is a bit disappointing is that Windows 8 and Windows Server 2012 do not come with cmdlets to permit me to run Windows Update from inside Windows PowerShell. A couple years ago, I wrote a series of scripts and functions that talk about using the Windows Update API to do this, but I do not want to mess around with a bunch of scripts. Luckily, I do not have to. Michal Gajda wrote a Windows Update PowerShell Module that is available from the Scripting Guys Script Repository. All I need to do is to download the module, unblock it, and copy it to a centralized location.

Download and unblock the module

The Windows Update PowerShell Module consists of a number of scripts. Once I download them, the PSWindowsUpdate.zip file is blocked. There are Windows PowerShell cmdlets now that will unblock files, but it is easier to use the graphical tool from Windows Explorer, if I download the script from Internet Explorer, as shown here.

Image of PSWindowsUpdate.zip Properties dialog box

Now that I have unblocked the archive, I extract it and copy the contents to a server with a shared folder exposed. This command is shown here.

Copy-Item -destination \\dc1\Share\PSWindowsUpdate -Recurse -Path C:\fso\PSWindowsUpdate -Force

Import the shared module

Now I need to import the shared module. This command makes sense, but is a bit stealth … that is, it is not really intuitive. Instead of simply supplying a module name, I include the complete path. I need to run the Windows PowerShell console with admin rights because the commands require admin rights to update my computer. Remember, modules are addressed by their folders that have the same name as the module itself; therefore, I simply type the path to the shared folder containing the module. The good thing is that tab expansion works and will fill out the path for me (to avoid misspelling of path names). The command is shown here.

ipmo \\dc1\Share\PSWindowsUpdate

Once I have imported the module, I use the Get-Command cmdlet to view the commands available to me. This is shown here.

PS C:\> gcm -Module pswindows*

 

CommandType     Name                                               ModuleName

-----------     ----                                               ----------

Function        Add-WUOfflineSync                                  PSWindowsUpdate

Function        Get-WUHistory                                      PSWindowsUpdate

Function        Get-WUInstall                                      PSWindowsUpdate

Function        Get-WUInstallerStatus                              PSWindowsUpdate

Function        Get-WUList                                         PSWindowsUpdate

Function        Get-WURebootStatus                                 PSWindowsUpdate

Function        Get-WUServiceManager                               PSWindowsUpdate

Function        Get-WUUninstall                                    PSWindowsUpdate

Function        Remove-WUOfflineSync                               PSWindowsUpdate

Function        Test-ElevatedShell                                 PSWindowsUpdate

The command (advanced function) I want is called Get-WUInstall. There is complete comment-based Help, and the advanced function is extremely powerful. I recommend spending some time getting familiar with the options. By default, it connects to Windows Update, presents a listing of available updates, and prompts to accept. (There is an acceptall switch that will automatically accept the updates). On my computer, I ran the advanced function with defaults, and found a video driver I did not know existed. This is shown here.

PS C:\> Get-WUInstall 

Confirm

Are you sure you want to perform this action?

Performing operation "nVidia - Graphics Adapter WDDM1.1, Graphics Adapter WDDM1.2,

Other hardware - NVIDIA Quadro 2000M[226 MB]?" on Target "EDLT".

[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help

(default is "Y"):y

 

X Status     KB          Size Title

- ------     --          ---- -----

2 Accepted             226 MB nVidia - Graphics Adapter WDDM1.1, Graphics Adapter...

3 Downloaded           226 MB nVidia - Graphics Adapter WDDM1.1, Graphics Adapter...

4 Installed            226 MB nVidia - Graphics Adapter WDDM1.1, Graphics Adapter...

 

If I want to know if I need to reboot the computer, I use the Get-WURebootStatus function. This is shown here.

PS C:\> Get-WURebootStatus

Reboot is not Required.

If I need to perform a reboot on Windows 8 and Windows Server 2012, I can call the Restart-Computer cmdlet. But even on Windows 7 (or Windows XP, for that matter) running Windows PowerShell 2.0 and the Get-WUInstall advanced function, there are switches for reboot. For example, there is the AutoReboot switch as well as the IgnoreReboot switch.

Now I can configure my remote servers and use Windows Update to perform the updates. Cool. It think it will require a bit of scripting to be on the safe side, but the cool thing about Windows PowerShell is that if something does not exist by default, someone in the community generally steps up to the plate. Thanks to Michal Gajda for writing and for sharing a cool module.

Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Run this module on Windows 7, it works very well. However, this module does not work on Windows 8 (I run it on two of my Windows 8 laptop, both failed). Under Windows 8, after import the module, I tried to check the command by issue the Get-Command -Module PSWindowsUpdate, I only got one of the command: "Test-ElevatedShell". Can someone help me with this?

    Thanks!

  • Hi,

    I solved the problem. When I import the module under Windows 8, I must use -force parameter. So the import command looks like:

    Import-Module PSWindowsUpdate -force

    After this command, I can get all the commands related to this module and can install windows updates.

    This is a great module!

  • @WYN thank you for your comments. Yes, it is a great module, and it really simplifies a lot of complicated code. I am glad you find it useful.

  • Trying to use this module from a Windows 7 box and remotely download updates on a Windows 2003 server. When I use the Get-WUlist command I can use the -computername switch but when I use the Get-WUInstall it does NOT have that switch. Why is that? Or am I do something wrong?

  • @Rob - you cannot remotely install updates with WU CmdLet.  See WU documentation for restrictions.

  • This is by design for the Windows Update Agent COM object and the IUpdateSession interface.

    msdn.microsoft.com/.../aa386863(v=vs.85).aspx

  • Even though i use -force with the Import-Module command, I get the following error:

    SecurityError: (:) [], PSSecurityException

    UnauthorizedAcess

    I get that It might have something to do with rights, but I'm running PowerShell with Admin rights?

    OS: Windows 8.1 Core

    64-bit

  • When updating Windows 2008 R2 Datacenter, the IE8 upgrade halts the whole upgrade process with a dialog box. Any way around that?