title: Downloading Windows Updates from WSUS server
body: <p>My goal:
  A client PC (Windows 10 Pro) automatically downloads Windows Updates from WSUS server and installs them, using Powershell.</p>
  
  <p>My situation:
  The client PC has joined an AD domain, and a GPO which designates WSUS server's IP address (http://192.168.1.200:8530) is applied to the PC.</p>
  
  <p>My Problem:
  I created the following Powershell script, and run it as Administrator on the PC.</p>
  
  <pre><code># 1. Start Windows Update session
  $updateSession = New-Object -com Microsoft.Update.Session
  
  # 2. Search Windows Updates
  $searcher = $updateSession.CreateUpdateSearcher()
  $searchResult = $searcher.search("IsInstalled=0 and Type='software'")
  
  # 3. Show the result of Windows Updates
  Write-Host -ForegroundColor Cyan "Downloadable Windows Updates are as follows:"
  $searchResult.Updates | % { $_.title -replace ".*(KB\d+).*", "`$1`t$&amp;" }
  
  # 4. Show the result of Windows Updates which are automatically downloadable
  $updatesToDownload = New-Object -com Microsoft.Update.UpdateColl
  $searchResult.Updates | ? { -not $_.InstallationBehavior.CanRequestUserInput } | ? { $_.EulaAccepted } | % { [void]$updatesToDownload.add($_) }
  
  # 5. Download
  Write-Host -ForegroundColor Cyan "Downloading updates..."
  $downloader = $updateSession.CreateUpdateDownloader()
  $downloader.Updates = $updatesToDownload
  $downloader.Download()
  
  # 6. Show the result of the downloaded updates
  Write-Host -ForegroundColor Cyan "Download is finished."
  $updatesToInstall = New-Object -com Microsoft.Update.UpdateColl
  $searchResult.Updates | ? { $_.IsDownloaded } | % { [void]$updatesToInstall.add($_) }
  
  # 7. Install
  Write-Host -ForegroundColor Cyan "Installing Updates..."
  $installer = $updateSession.CreateUpdateInstaller()
  $installer.Updates = $updatesToInstall
  $installationResult = $installer.Install()
  
  # 8. Show the result of installation (ResultCode 2 is success, over 3 is fail.)
  $installationResult
  
  # 9. reboot
  Restart-Computer
  </code></pre>
  
  <p>The above script seemed working, but it turned out the clients downloaded updates from the Internet (maybe Microsoft Update Site?). In fact, even when I shut down the WSUS server, this script worked. Downloading and installation were successful.</p>
  
  <p>My Question:
  Which part of the script is wrong? Or I can't download updates from WSUS using Powershell?</p>