I am new to PowerShell. What would be the proper command to delete all files
older than one hour within a specific folder?
Thank you,
Max
gci |? { [DateTime]::Now - $_.LastWriteTime -lt [TimeSpan]::FromHours(1) } | ri
Nick
$now = get-date
get-childitem * |
where-object {$_.LastWriteTime -le $now.AddHours(-1)} |
remove-item -whatif
Once you are sure that the command does what you want repeat without
the -whatif parameter.
Andrew Watt MVP
I think your code removes the wrong files. It looks as if it deletes
files that are less than 1 hour old. The questioner wanted to remove
files more than one hour old.
As a general piece of advice, when using remove-item always test using
the -whatif parameter. PowerShell may protect you anyway. No point in
taking risk in my opinion.
Andrew Watt MVP
I am going to put this in a .ps1 run it every hour by using scheduled task
job. Would that be fine or I need some special configurations?
Thank you again,
Max
"Andrew Watt [MVP]" <SVGDev...@aol.com> wrote in message
news:umdnq25cpp96gbe26...@4ax.com...
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -Command
path_to_your_script.ps1
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Yeah, you should always tack on '-whatif' before you run something potentially hazardous.
Nick
>Doh! I must have misread the question. Or else I'm just careless.
>
>Yeah, you should always tack on '-whatif' before you run something potentially hazardous.
>
>Nick
<grin/> Don't worry about it. I've done it before and probably will do
it again. :(
Andrew Watt MVP