Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting all files older than one hour in a folder

739 views
Skip to first unread message

Maxwell2006

unread,
Jan 15, 2007, 11:16:17 AM1/15/07
to
Hi,

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


Nick Howell

unread,
Jan 15, 2007, 12:07:23 PM1/15/07
to
Something like this?

gci |? { [DateTime]::Now - $_.LastWriteTime -lt [TimeSpan]::FromHours(1) } | ri


Nick

Andrew Watt [MVP]

unread,
Jan 15, 2007, 12:17:34 PM1/15/07
to
The following should do what you want:

$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

Andrew Watt [MVP]

unread,
Jan 15, 2007, 12:19:19 PM1/15/07
to
Nick,

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

Maxwell2006

unread,
Jan 15, 2007, 4:35:32 PM1/15/07
to

Thank you very much Andrew for help.
This is exactly what I was looking for.

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...

RichS

unread,
Jan 15, 2007, 4:51:01 PM1/15/07
to
Remember that .ps1 files won't automatically run like vbscript files would.
You need to ensure your schduled task command looks something like this

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

Nick Howell

unread,
Jan 16, 2007, 10:21:26 PM1/16/07
to
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

Andrew Watt [MVP]

unread,
Jan 17, 2007, 3:40:54 AM1/17/07
to
On Tue, 16 Jan 2007 21:21:26 -0600, Nick Howell
<msnews.1...@spamgourmet.com> wrote:

>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

0 new messages