Windows PowerShell - Delete Files Older than X Days
<p>I am currently new at PowerShell and I have created a script based on gathered information on the net that will perform a Delete Operation for found files within a folder that have their LastWriteTime less than 1 day.</p>

<p>Currently the script is as follows:</p>

<pre><code>$timeLimit = (Get-Date).AddDays(-1) 
$oldBackups = Get-ChildItem -Path $dest -Recurse -Force -Filter "backup_cap_*" |
                Where-Object {$_.PSIsContainer -and $_.LastWriteTime -lt $timeLimit}

foreach($backup in $oldBackups)
{
    Remove-Item $dest\$backup -Recurse -Force -WhatIf
}
</code></pre>

<p>As far as I know the <code>-WhatIf</code> command will output to the console what the command "should" do in real-life scenarios. The problem is that <code>-WhatIf</code> does not output anything and even if I remove it the files are not getting deleted as expected.</p>

<p>The server is Windows 2012 R2 and the command is being runned within PowerShell ISE V3.</p>

<p>When the command will work it will be "translated" into a task that will run each night after another task has finished backing up some stuff.</p>