title: Windows Update Agent API - Searching for Updates
body: <p>I wrote a Powershell script that uses the Windows Update Agent API (IUpdateSearcher, IUpdateDownloader, IUpdateInstaller etc.). Everything works fine, the script finds avaiable updates, downloads and installs them.</p>
  
  <p>However, there is a problem when searching for consecutive updates. For example, there is an update for the .Net Framework 4.5.2. The update is installed by script and the PC is rebooted afterwards. Now there should be an update for the .Net Framework 4.5.2 Language Pack avaiable.</p>
  
  <p>But it is not. At least not via the API. A manual search with the GUI (Windows Update) works.</p>
  
  <p>After the manual search, the API finds the update a well! </p>
  
  <p>What am I missing in my script? I could not find anything in Microsofts documentation: <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa386868(v=vs.85).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/desktop/aa386868(v=vs.85).aspx</a></p>
  
  <pre><code>$updateSession = New-Object -ComObject 'Microsoft.Update.Session'
  $UpdateSession.WebProxy.AutoDetect = $false
  
  $updateSearcher = $updateSession.CreateUpdateSearcher()
  
  $searchResult = $updateSearcher.Search('IsInstalled=0 and IsHidden=0')
  
  $objCollectionDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
  
  foreach ($update in $searchResult.Updates)
  {
      $objCollectionTmp = New-Object -ComObject 'Microsoft.Update.UpdateColl'
      $objCollectionTmp.Add($update) | Out-Null
  
      $downloader = $updateSession.CreateUpdateDownloader()
      $downloader.Updates = $objCollectionTmp
  
      try
      {
          $downloadResult = $downloader.Download()
      }
      catch
      {
          //exception Handling
      }
  
      $objCollectionDownload.Add($update) | Out-Null
  }
  
  $updatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'
  $updateInstaller = $updateSession.CreateUpdateInstaller()
  
  foreach ($update in $objCollectionDownload)
  {
      //accept Eula etc...
  
      $updatesToInstall.Add($update) | Out-Null
  }
  
  $updateInstaller.Updates = $updatesToInstall
  
  $installationRestult = $updateInstaller.Install()
  
  //check installation result
  </code></pre>