Building pauses in-between objects in commands

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.

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"}"
by MattG at 2012-12-03 14:50:33
You might consider sleeping in between each deletion:
Get-ChildItem "\gha01sdp333\BigBackups" *.bak -Recurse
| Where-Object {$.LastWriteTime -le "$TODATE"}
| ForEach-Object { Remove-Item $
; Start-Sleep 10 }
This will cause PowerShell to sleep for ten seconds after each call to Remove-Item.
by i255d at 2012-12-03 20:23:55
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.

Get-ChildItem -Path \\Users\Test -Recurse
| Where-Object {$.LastWriteTime -le $ToDate}
| ForEach-Object { Remove-Item -Path \\Users\Test$
; Start-Sleep -Seconds 10 }

This seems to work in my testing.
by nohandle at 2012-12-04 05:10:24
Hi, responded on your question on powershell com forum also.
double piping the output with where and foreach is unnecessary.
try this: Get-ChildItem -Path \\Users\Test -Recurse |
ForEach-Object {
if ($.LastWriteTime -le $ToDate)
Remove-Item -Path $
.fullname -Recurse -Force
Start-Sleep -Seconds 10
by nohandle at 2012-12-04 05:14:21
Just for the record,
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 :smiley:
by i255d at 2012-12-04 11:14:02
Final answer - provided by nohandle

Get-ChildItem -Path '\\QCBACKUPS' -Include .txt,.tst -Recurse |
.LastWriteTime -le (Get-Date).adddays(-4))
Remove-Item -Path $_.fullname -Recurse -Force
Start-Sleep -Seconds 60