title: How to correct this PowerShell script that auto deletes Sharepoint backups
body: <p>I've <a href="http://imperfectit.blogspot.com/2010/03/automate-sharepoint-2010-farm-backups.html" rel="nofollow">been using this</a> (<a href="http://blog.wauwwie.nl/Lists/Posts/Post.aspx?ID=4" rel="nofollow">and this</a>) script to delete older sharepoint backups, but it deletes all backups rather than the 14+ day old ones. </p>
  
  <p>I ran it through powershell_ise.exe and put a break point under the line that has <code>$_.SPStartTime</code> in it, and it shows <code>$_.SPStartTime =</code> as if the date isn't populated. I looked inside <code>$sp.SPBackupRestoreHistory.SPHistoryObject</code> and that contains the data I expect.</p>
  
  <p>The part that is there issue is on this line:</p>
  
  <pre><code># Find the old backups in spbrtoc.xml
  $old = $sp.SPBackupRestoreHistory.SPHistoryObject |
  ? { $_.SPStartTime -lt ((get-date).adddays(-$days)) }
  </code></pre>
  
  <p>I get all of the dates output (which I would expect). This tells me the problem in in the 'where' or '?' - I understand they are interchangeable. Regardless, $old always appears to be null.</p>
  
  <p>As Requested:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;SPBackupRestoreHistory&gt;
      &lt;SPHistoryObject&gt;
          &lt;SPId&gt;a8a03c50-6bc2-4af4-87b3-caf60e750fa0&lt;/SPId&gt;
          &lt;SPRequestedBy&gt;ASERVER\AUSER&lt;/SPRequestedBy&gt;
          &lt;SPBackupMethod&gt;Full&lt;/SPBackupMethod&gt;
          &lt;SPRestoreMethod&gt;None&lt;/SPRestoreMethod&gt;
          &lt;SPStartTime&gt;01/09/2011 00:00:13&lt;/SPStartTime&gt;
          &lt;SPFinishTime&gt;01/09/2011 00:05:22&lt;/SPFinishTime&gt;
          &lt;SPIsBackup&gt;True&lt;/SPIsBackup&gt;
          &lt;SPConfigurationOnly&gt;False&lt;/SPConfigurationOnly&gt;
          &lt;SPBackupDirectory&gt;E:\Backups\spbr0003\&lt;/SPBackupDirectory&gt;
          &lt;SPDirectoryName&gt;spbr0003&lt;/SPDirectoryName&gt;
          &lt;SPDirectoryNumber&gt;3&lt;/SPDirectoryNumber&gt;
          &lt;SPTopComponent&gt;Farm&lt;/SPTopComponent&gt;
          &lt;SPTopComponentId&gt;689d7f0b-4f64-45d4-ac58-7ab225223625&lt;/SPTopComponentId&gt;
          &lt;SPWarningCount&gt;0&lt;/SPWarningCount&gt;
          &lt;SPErrorCount&gt;0&lt;/SPErrorCount&gt;
      &lt;/SPHistoryObject&gt;
      &lt;SPHistoryObject&gt;
          &lt;SPId&gt;22dace04-c300-41d0-a9f1-7cfe638809ef&lt;/SPId&gt;
          &lt;SPRequestedBy&gt;ASERVER\AUSER&lt;/SPRequestedBy&gt;
          &lt;SPBackupMethod&gt;Full&lt;/SPBackupMethod&gt;
          &lt;SPRestoreMethod&gt;None&lt;/SPRestoreMethod&gt;
          &lt;SPStartTime&gt;01/08/2011 00:00:13&lt;/SPStartTime&gt;
          &lt;SPFinishTime&gt;01/08/2011 00:05:26&lt;/SPFinishTime&gt;
          &lt;SPIsBackup&gt;True&lt;/SPIsBackup&gt;
          &lt;SPConfigurationOnly&gt;False&lt;/SPConfigurationOnly&gt;
          &lt;SPBackupDirectory&gt;E:\Backups\spbr0002\&lt;/SPBackupDirectory&gt;
          &lt;SPDirectoryName&gt;spbr0002&lt;/SPDirectoryName&gt;
          &lt;SPDirectoryNumber&gt;2&lt;/SPDirectoryNumber&gt;
          &lt;SPTopComponent&gt;Farm&lt;/SPTopComponent&gt;
          &lt;SPTopComponentId&gt;689d7f0b-4f64-45d4-ac58-7ab225223625&lt;/SPTopComponentId&gt;
          &lt;SPWarningCount&gt;0&lt;/SPWarningCount&gt;
          &lt;SPErrorCount&gt;0&lt;/SPErrorCount&gt;
      &lt;/SPHistoryObject&gt;
  &lt;/SPBackupRestoreHistory&gt;
  </code></pre>