• Around The HomeToggle Menu

    • Entertainment
    • Productivity
    • Smart Home
  • FamilyToggle Menu

    • Parenting
    • Toys
    • Pets
    • Travel
  • Product ReviewsToggle Menu

    • Phones
    • Tablets
    • Laptops
    • Desktops
    • Wearables
    • Audio
    • Cameras
    • Headphones
    • Printers
    • Smart Home
    • TVs
    • Gaming and Video
  • One Cool ThingToggle Menu

    • Frugal Tech
    • Kickstarters
    • Videos
Techwalla
  1. Home
  2. Around The Home
  3. Productivity
  4. How to Use PowerShell to Run Windows Updates

How to Use PowerShell to Run Windows Updates

March 31, 2015
By: Jacob Reis
  • Share
  • Share on Facebook

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.

Video of the Day

...
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.

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

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

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

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

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

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

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

$needsReboot = $installResult.rebootRequired
}

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.

Show Comments

Related Articles

What Is CGI Access for cPanel?

What Is CGI Access for cPanel?

Around The Home
Productivity
By: Jimmy Boyd
How to Split a String Into Two Variables in PowerShell

How to Split a String Into Two Variables in PowerShell

Around The Home
Productivity
By: Gareth Downes-Powell
My Home Is Smarter Than Your Home

My Home Is Smarter Than Your Home

Around The Home
Smart Home
By: David Isaac
How to Kill the Task Manager With the Windows Command Line

How to Kill the Task Manager With the Windows Command Line

Around The Home
Productivity
By: Gareth Downes-Powell
How to Make a Web Bot

How to Make a Web Bot

Around The Home
Productivity
By: Peter Grant
How to Run a Script on a Web Page

How to Run a Script on a Web Page

Around The Home
Productivity
By: Dan Stone
  • HOW WE SCORE
  • ABOUT US
  • CONTACT US
  • TERMS
  • PRIVACY POLICY
  • COPYRIGHT POLICY
  • Advertise

An error occurred. Try again later.

Thanks for signing up!
© 2018 Leaf Group Ltd. Leaf Group Media

Get great tech advice delivered to your inbox.

Keep your family productive, connected, entertained, and safe.

Please enter a valid email.