Use a PowerShell Module to Run Windows Update
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.
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.
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
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy