How to Use PowerShell to Run Windows Updates

Computers exist to make our lives easier. They can do things such as calculate advanced equations and help us contact those who are thousands of miles away. Another way computers can make our lives easier is simply by making it easier to use the computer. For example, computers nowadays can be scripted to perform long lists of tasks with very little input at all by the user. Windows PowerShell is one such automation program. It can do things such as install Windows Updates without the hassle that it takes normally.

...
Programming scripts with Windows PowerShell can help automate many functions.

Step

Create a new Notepad file. Hit the Windows icon in the bottom left-hand corner of your screen and type Notepad to open a new document.

Step

Copy-and-paste the following text into the document. Save the document as "WindowsUpdate.ps1" without the quotes. Close the document.

Step

function Get-WIAStatusValue($value) { switch -exact ($value) { 0 {"NotStarted"} 1 {"InProgress"} 2 {"Succeeded"} 3 {"SucceededWithErrors"} 4 {"Failed"} 5 {"Aborted"} } }

Step

$needsReboot = $false $UpdateSession = New-Object -ComObject Microsoft.Update.Session $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()

Step

Write-Host " - Searching for Updates" $SearchResult = $UpdateSearcher.Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0")

Step

Write-Host " - Found [$($SearchResult.Updates.count)] Updates to Download and install" Write-Host

Step

foreach($Update in $SearchResult.Updates) { # Add Update to Collection $UpdatesCollection = New-Object -ComObject Microsoft.Update.UpdateColl if ( $Update.EulaAccepted -eq 0 ) { $Update.AcceptEula() } $UpdatesCollection.Add($Update) | out-null

Step

Download Write-Host " + Downloading Update $($Update.Title)" $UpdatesDownloader = $UpdateSession.CreateUpdateDownloader() $UpdatesDownloader.Updates = $UpdatesCollection $DownloadResult = $UpdatesDownloader.Download() $Message = " - Download {0}" -f (Get-WIAStatusValue $DownloadResult.ResultCode) Write-Host $message

Step

Install Write-Host " - Installing Update" $UpdatesInstaller = $UpdateSession.CreateUpdateInstaller() $UpdatesInstaller.Updates = $UpdatesCollection $InstallResult = $UpdatesInstaller.Install() $Message = " - Install {0}" -f (Get-WIAStatusValue $DownloadResult.ResultCode) Write-Host $message Write-Host

Step

$needsReboot = $installResult.rebootRequired
}

Step

if($needsReboot) { restart-computer }

Step

Press the Windows button again and type "powershell" without the quotes. Right-click the Windows PowerShell icon that appears in the search box and click "Run as Administrator."

Step

Type the following if this is your first time running a Windows PowerShell script: "set-executionpolicy remotesigned." This will allow Windows PowerShell to run scripts that you write yourself or that are signed by a trusted publisher. Press enter. Type "Y" to confirm and press "Enter."

Step

Type the full file location of the document you have just created into the PowerShell window. For example, you would type "C:\users\name\documents\windowsupdate.ps1."

Step

Press "Enter." The script will now execute and updates will be found, downloaded and installed with no further input from you. You can run this script as often as you like to check for and download new Windows updates. Your computer may prompt you to restart after updates are finished.