by i255d at 2012-12-03 13:56:01
I am runnin a script that is deleting backup over 4 days old. The files are so large that it is casuing problems on my storage when it starts to delete to many at once. I want to build a pause into my script inbetween objects ($.) as they are piped through the commands. I hope I am explainin this well.by MattG at 2012-12-03 14:50:33$CURRENTDATE = Get-Date
$TODATE = $CURRENTDATE.AddDays(-4)
get-childitem "\gha01sdp333\BigBackups" *.bak -recurse | where {$
.LastWriteTime -le "$TODATE"} | Remove-Item
get-childitem "\gha01sdp333\BigBackups" *.trn -recurse | where {$.LastWriteTime -le "$TODATE"} | Remove-Item
Each *.bak file is hundreds of GB, so I have been asked to find a way to create a delay between each Item before or after it is put in the "where {$.LastWriteTime -le "$TODATE"}"
You might consider sleeping in between each deletion:by i255d at 2012-12-03 20:23:55Get-ChildItem "\gha01sdp333\BigBackups" *.bak -Recurse
This will cause PowerShell to sleep for ten seconds after each call to Remove-Item.
| Where-Object {$.LastWriteTime -le "$TODATE"}
| ForEach-Object { Remove-Item $; Start-Sleep 10 }
Thanks for the big help! The only problem is that the path does’t get passed through so it fails when it tries to delete it.by nohandle at 2012-12-04 05:10:24Get-ChildItem -Path \192.168.1.67\Users\Test -Recurse
| Where-Object {$.LastWriteTime -le $ToDate}
| ForEach-Object { Remove-Item -Path \192.168.1.67\Users\Test$; Start-Sleep -Seconds 10 }
This seems to work in my testing.
Hi, responded on your question on powershell com forum also.by nohandle at 2012-12-04 05:14:21
double piping the output with where and foreach is unnecessary.
try this:Get-ChildItem -Path \192.168.1.67\Users\Test -Recurse |
ForEach-Object {
if ($.LastWriteTime -le $ToDate)
{
Remove-Item -Path $.fullname -Recurse -Force
Start-Sleep -Seconds 10
}
}
Just for the record,by i255d at 2012-12-04 11:14:02
accomplishing the task just by the where-object filter is possible but not recommended solution. The purpose of the command is obfuscated by the unusual usage of the filtering condition.Get-childItem . *.txt | where{((-not(sleep 1)) -and ($.LastWriteTime -le (get-date).adddays(-10)))}
Explaining how this works on another forum I realized that if the conditions are switched (sleep after the and, not before) the first condition should evaluate to false on files that are not meant to be deleted and the whole condition should therefore evaluate to false without running the sleep command. Hence not waiting on every file but just on files to be deleted. Have to test it though.
Dips on writing an article about this
Final answer - provided by nohandleGet-ChildItem -Path '\192.168.1.1\QCBACKUPS' -Include .txt,.tst -Recurse |
ForEach-Object{
if($.LastWriteTime -le (Get-Date).adddays(-4))
{
Remove-Item -Path $_.fullname -Recurse -Force
Start-Sleep -Seconds 60
}
}