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.
Currently the script is as follows:
$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
}
As far as I know the -WhatIf
command will output to the console what the command "should" do in real-life scenarios. The problem is that -WhatIf
does not output anything and even if I remove it the files are not getting deleted as expected.
The server is Windows 2012 R2 and the command is being runned within PowerShell ISE V3.
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.
$PSIsContainer
? 3) If that's your script, where is $dest coming from? If it's not defined, gci will read the current working directory without error. 4) Have you confirmed that$oldBackups
has anything in it? 5) DoesRemove-Item
throw any exceptions about files not existing? - TessellatingHeckler