How to Use PowerShell to Run Windows Updates

Written by jacob reis Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use PowerShell to Run Windows Updates
Programming scripts with Windows PowerShell can help automate many functions. (Ablestock.com/AbleStock.com/Getty Images)

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.

Skill level:
Moderately Easy

Other People Are Reading

Instructions

  1. 1

    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.

  2. 2

    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"}
    

    }

    }

    $needs Reboot = $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
    

    }

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    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.

Tips and warnings

  • Keep watching the PowerShell window to check the progress of your updates.
  • Do not download or run scripts that you do not trust. You could seriously damage your computer.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.