title: Get the last Windows Update install date using PowerShell
body: <p>I am trying to write a PowerShell script that will query all the servers in Active Directory and see the last date a Windows Update was applied.  </p>
  
  <p>I was having some trouble, so just to get it done, I created two scripts, one in Powershell to get the servers and the other in VBScript to query the last date.  I found a this Powershell <a href="http://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc" rel="nofollow">module</a> that allows me to query the last install date, but it is <em>extremely</em> slow, especially on remote servers.</p>
  
  <p>Here is the PS code:</p>
  
  <pre><code>Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties * |
  Select-Object Name | Sort-Object Name |
  ForEach-Object {
      Get-WUHistory -ComputerName $_.Name | Sort-Object Date,ComputerName -Descending |
      Select-Object -First 1
  }
  </code></pre>
  
  <p>Its so slow, its practically unusable.</p>
  
  <p>I have some VBScript which I cobbled together that is <em>much</em> faster.  See below:</p>
  
  <pre><code>On Error Resume Next
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set file = fso.OpenTextFile ("servers.csv", 1)
  server = ""
  
  Do Until file.AtEndOfStream
    line = file.Readline
    server = line
    'wscript.echo server
    Set objSession = CreateObject("Microsoft.Update.Session", server)
    If Err.Number &lt;&gt; 0 Then
      'WScript.Echo server &amp; " Error: " &amp; Err.Number &amp; " Error (Hex): " &amp; Hex(Err.Number) &amp; " Source: " &amp;  Err.Source &amp; " Description: " &amp;  Err.Description
      WScript.Echo server &amp; " Communications Error"
      Err.Clear
    Else
        Set objSearcher = objSession.CreateUpdateSearcher
        Set colHistory = objSearcher.QueryHistory(1, 1)
        For Each objEntry in colHistory
          Wscript.Echo server &amp; " " &amp; objEntry.Date
        Next
    End If
  Loop
  
  file.Close
  </code></pre>
  
  <p>Is there an easy way to get the speed of the VBScript into the Powershell code?</p>
  
  <hr>
  
  <p>Here is the working Powershell code (modified again) if anyone is interested:</p>
  
  <pre><code>$ErrorActionPreference= 'silentlycontinue'
  Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties * | Select-Object Name |
  ForEach-Object {
      If (Test-Connection $_.Name -Count 1){
          Get-HotFix -ComputerName $_.Name | Sort-Object InstalledOn -Descending | Select-Object -First 1 
      }
      else {
          Write-host $_.Name " Connection Error"
      }
  } |
  Sort-Object InstalledOn
  </code></pre>